【发布时间】:2017-07-16 01:27:00
【问题描述】:
我对 GUI 不太熟悉,这是我第一次尝试。我为我的 A* 算法创建了一个 GUI。它被设置为 15 个 JButton 的网格。我遇到的问题是当我重置 GUI(重置按钮)时,除非我单击另一个 JButton,否则背景不会完全改变。不用说,我被难住了。
编辑: 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);
}
}
}
});
}
}
这是图形用户界面
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强制更新 -
“背景没有完全改变” 当按下
Reset按钮时,我得到了“很多改变”。你到底看到了什么,你期望看到什么? -
参见
resetGame()引用的示例here。 -
顺便说一句:如果没有点击,为什么要使用 225 个按钮?
标签: java multithreading swing user-interface