【发布时间】:2011-10-18 04:34:53
【问题描述】:
我的 gui 是一个 50x50 的 GridLayout,它使用 SwingWorker 进行更新。
GridLayout 中的每个网格都有一个具有特定强度的 GridGraphic 组件。
默认强度 = 0,这只是一个黑色分量。如果强度 = 5,则网格显示为黑色并带有一个黄点。
当按下 Step 按钮时,StepManager 应该遍历所有网格并更新它们的强度,然后立即重新绘制所有内容,但 StepManager 在第一个具有强度 = 5 的值的 GridGraphic 之后停止执行。
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();
}
});
}
}
【问题讨论】:
标签: user-interface swingworker