GUI没有按预期更新

时间:2017-02-25 03:53:26

标签: java multithreading swing user-interface

我不熟悉GUI,这是我第一次尝试。我为我的A *算法创建了一个GUI。它被设置为15个JButtons的网格。我遇到的问题是当我重置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);
                        }
                    }
            }
        });
    }
}

这是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重新进入“监听”之后,JButtons的背景正在被设置。我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

我对class HintViewController: UIViewController, UIWebViewDelegate 课程进行了一些更改。查看评论,不要犹豫,询问是否不清楚或不回答您的问题:

func webViewDidStartLoad(_ webView: UIWebView)
func webViewDidFinishLoad(_ webView: UIWebView)