java - GUI 没有按应有的方式更新

标签 java multithreading swing user-interface

我对 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/

相关文章:

java - 简单的java日期戳问题

java - ANT 可以在远程服务器上启动重启作业吗?

java - 获取 listPreference 选定的项目。安卓

Java 日志记录和 Unix Nohup 问题

java - 这个 "Image Fetcher 0"有什么作用?

java - java MYSQL 删除最后一行

Java 执行器通过内部执行器超时取消作业

java - 在 Java 中迭代地使用线程

c# - MVC 应用程序中的作业调度

java - 如何仅接受 JTextField 中的指定模式?