首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gui SwingWorker ...在重新绘制之前不更新所有内容

Gui SwingWorker ...在重新绘制之前不更新所有内容
EN

Stack Overflow用户
提问于 2011-10-18 12:34:53
回答 2查看 267关注 0票数 0

我的gui是一个使用SwingWorker更新的50x50 GridLayout。

GridLayout中的每个栅格都有一个具有特定强度的GridGraphic组件。

默认强度= 0,这只是一个黑色分量。如果强度为5,则栅格显示为带有黄点的黑色。

当按下Step按钮时,StepManager应该遍历所有栅格并更新它们的强度,然后立即重新绘制所有内容,但StepManager在第一个强度值为5的GridGraphic之后停止执行。

代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;


public class Gui extends JFrame{

    private JPanel buttonPanel, populationPanel, velocityPanel, gridPanel;
    private JButton setupButton, stepButton, goButton;
    private JLabel populationNameLabel, velocityNameLabel, populationSliderValueLabel, velocitySliderValueLabel;
    private JSlider populationSlider, velocitySlider;
    private GridGraphic [] [] gridGraphic;
    private int agents = 125;
    private int velocity = 500;
    private boolean resetGrid;
    private StepManager step;


    public Gui() {

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();


        //Set up JButtons
        buttonPanel = new JPanel();
        setupButton = new JButton("Setup");
        stepButton = new JButton("Step");
        goButton = new JButton("Go");

        buttonPanel.add(setupButton);
        buttonPanel.add(stepButton);
        buttonPanel.add(goButton);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 3;
        add(buttonPanel, c);




    public GridGraphic getGridGraphic(int n1, int n2){
        return gridGraphic[n1][n2];
    }


    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        Gui gui = new Gui();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        gui.pack();
        gui.setVisible(true);
        gui.setResizable(false);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
EN

回答 2

Stack Overflow用户

发布于 2011-10-18 12:40:43

我相信这个步骤是在一个单独的线程中执行的,所以当你在它上面调用execute方法时,它会在一个单独的线程中开始执行,这个线程可能会在你调用repaint方法之后完成执行。您想要的是在worker完成其任务后调用repaint。查看SwingWorker的done()方法。

票数 1
EN

Stack Overflow用户

发布于 2011-10-18 12:54:39

你会得到某种例外吗?

在代码中

代码语言:javascript
复制
   for(int i = 0; i < 50; i++){
        for(int j = 0; j < 50; j++){
            if(grid[i][j].getIntensity()==5){
                grid[i][j].setDefault();
                grid[i-1][j-1].setAgent();
            }
        }
    }

我可以看到ArrayIndexOutOfBoundsException,因为如果i或j为0,并且gridi和/或grid的强度为5,则可能会访问grid-1。

编辑:另一件事。在执行SwingWorker之后,您似乎只重绘了一次图形用户界面。但是,SwingWorker是一个线程,所以重画将在worker启动后立即执行,而不是在它完成后执行。尝试在您的done方法中调用repaint。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7802472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档