作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 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;
}
}
我的猜测是,JButton 的背景是在 ActionListener 返回“监听”状态后设置的。我只是不知道如何解决这个问题。
最佳答案
我对 Driver
类进行了一些更改。查看评论,如果不清楚或没有回答您的问题,请随时询问:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
public class Driver {
static Random rand = new Random();
private static final int GRID_SIZE = 15;
private int[][] a; //make it a field
private GUI run; //make it a field
Color defaultColor;
//add constructor
Driver(){
run = new GUI();
a = new int[GRID_SIZE][GRID_SIZE];
defaultColor = run.getBackground();
//the following code is used twice : use a method
reDrawGui();
run.getReset().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
reDrawGui();
}
});
}
/**
*
*/
private void reDrawGui() {
for(int x = 0; x < a.length; x++) {
for(int y = 0; y < a[x].length; y++)
{
a[x][y] = rand.nextInt(10);
JButton button = run.getGrid()[x][y];
switch(a[x][y])
{
case 4: button.setBackground(Color.MAGENTA);
break;
case 5: button.setBackground(Color.CYAN);
break;
case 7:
case 8:
case 9:button.setBackground(Color.BLACK);
break;
default : button.setBackground(defaultColor);
}
}
}
//revalidate and repaint after gui changed
run.revalidate(); run.repaint();
}
public static void main(String[ ] args) {
//move intialization code to constructor
new Driver();
}
}
关于java - GUI 没有按应有的方式更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451767/
当我运行我的应用程序时,我在控制台中收到一条消息: 2011-11-16 19:17:41.292 Juice[8674:707] Applications are expected to have
我在 JavaScript 中使用了这个语句,但是当我尝试在 TypeScript 项目中使用它时出现错误。它在提示 fetch(...args) const fetcher = (...args)
我是一名优秀的程序员,十分优秀!