1
我不熟悉GUI的那麼多,這是我的第一次嘗試之一。我爲我的A *算法創建了一個GUI。它被設置爲15個JButton的網格。我遇到的問題是,當我重置GUI(重置按鈕)時,背景沒有完全改變,除非我點擊另一個JButton。不用說,我很難過。GUI沒有更新,因爲它應該
編輯:網格的70-95%的背景改變爲它們的實際顏色。如果我點擊另一個按鈕,那麼100%的網格是正確的。我還注意到,如果我按住重置按鈕一段較長的時間,則更多的背景更改正確。
下面是我的問題的簡化版本。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.util.*;
public class Driver {
static Random rand = new Random();
public static void main(String[ ] args) {
final int GRID_SIZE = 15;
GUI run = new GUI();
int[][] a = new int[GRID_SIZE][GRID_SIZE];
for(int x = 0; x < a.length; x++)
for(int y = 0; y < a[x].length; y++)
{
a[x][y] = rand.nextInt(10);
switch(a[x][y])
{
case 4: run.getGrid()[x][y].setBackground(Color.MAGENTA);
break;
case 5: run.getGrid()[x][y].setBackground(Color.CYAN);
break;
case 7:
case 8:
case 9: run.getGrid()[x][y].setBackground(Color.BLACK);
break;
}
}
run.getReset().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int x = 0; x < a.length; x++)
for(int y = 0; y < a[x].length; y++)
{
a[x][y] = rand.nextInt(10);
switch(a[x][y])
{
case 4: run.getGrid()[x][y].setBackground(Color.MAGENTA);
break;
case 5: run.getGrid()[x][y].setBackground(Color.CYAN);
break;
case 7:
case 8:
case 9: run.getGrid()[x][y].setBackground(Color.BLACK);
break;
default: run.getGrid()[x][y].setBackground(null);
}
}
}
});
}
}
這裏的GUI
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.LineBorder;
public class GUI extends JFrame {
private final int WINDOW_WIDTH, WINDOW_HEIGHT, GRID_SIZE;
private JButton[][] grid;
private JButton reset;
private GridBagLayout gbl;
private GridBagConstraints gbc;
public GUI(){
GRID_SIZE = 15;
WINDOW_WIDTH = 500;
WINDOW_HEIGHT = 500;
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
reset = new JButton("Reset");
grid = new JButton[this.GRID_SIZE][this.GRID_SIZE];
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(gbl);
this.setSize(this.WINDOW_WIDTH, this.WINDOW_HEIGHT);
this.buildButtons();
this.setVisible(true);
}
private void buildButtons()
{
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = .5;
for(int i = 0; i < grid.length; i++)
{
gbc.ipady = this.WINDOW_HEIGHT/(this.GRID_SIZE * 3);
for(int j = 0; j < grid[i].length; j++)
{
grid[i][j] = new JButton();
grid[i][j].setOpaque(true);
grid[i][j].setBorder(LineBorder.createBlackLineBorder());
grid[i][j].setText(i*this.GRID_SIZE+j+"");
gbc.gridx = i;
gbc.gridy = j+1;
this.add(grid[i][j], gbc);
}
}
gbc.gridx = 0;
gbc.weighty = .5;
gbc.gridy = this.GRID_SIZE+1;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.ipady = this.WINDOW_HEIGHT/(this.GRID_SIZE + 2);
gbc.gridwidth = this.GRID_SIZE;
this.add(reset, gbc);
}
public JButton[][] getGrid()
{
return this.grid;
}
public JButton getReset()
{
return this.reset;
}
}
我的猜測是的ActionListener返回到‘聽’後,這些Jbutton背景被設定。我只是不知道如何解決這個問題。
呼叫'revalidate'和'repaint'迫使它更新 – MadProgrammer
*「的背景唐'完全改變'*按'重置'按鈕時,我會得到'很多改變'。你究竟看到了什麼,你期望看到什麼? –
請參閱引用的示例[here](http://stackoverflow.com/a/10326636/230513)中的'resetGame()'。 – trashgod