gpt4 book ai didi

java - 从 Controller 方法更新面板

转载 作者:行者123 更新时间:2023-12-02 03:03:24 25 4
gpt4 key购买 nike

我想从我的 Controller 更新我的主机上的面板(在类 GUI 中)。基本上,每个模拟步骤都应该更新面板。

    for (int step = 0; step < simSteps; step++){
this.gui.updateGUI(this.stats, step);
}

在我的 GUI 类中,方法 updateGUI(...) 如下所示:

 public void updateGUI(Stats stats, int step){
this.stats = stats;
getContentPane().remove(pollutionPanel);
pollutionPanel = new BarPanel(settings, stats, possibleColors);
getContentPane().add(pollutionPanel);
validate();
repaint();
}

我的 BarPanel 类重写了paintComponent 方法,我只是使用图形对象来绘制一些东西。开始模拟时,面板仅在最后一个模拟步骤后更新,尽管我已经知道在每个模拟步骤中都会调用 updateGUI() 方法(我做了一些调试输出)。

有人知道为什么会发生这种情况吗?

最佳答案

所以,基本概念是,您想要生成一些动画,UI 在一段时间内更新。这并不是一个罕见的请求,有很多例子。

注意事项:

  • Swing 是单线程的
  • 不要做任何可能阻塞事件调度线程的事情,这将阻止 UI 更新,直到控制权传回给它为止
  • 不要在 EDT 上下文之外更新 UI

好吧,这听起来有点矛盾,但是 Swing 提供了一些工具来让您的生活更轻松

你可以...

使用SwingWorker

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public interface StepContainer {

public void addStep(int index);
}

public class TestPane extends JPanel implements StepContainer {

private int step = 0;

public TestPane() {
setLayout(new GridBagLayout());
PopulateWorker worker = new PopulateWorker(10, this);
worker.execute();
}

@Override
public void addStep(int step) {
JButton btn = new JButton(Integer.toString(step));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

add(btn, gbc);
revalidate();
repaint();
}

}

public class PopulateWorker extends SwingWorker<Void, Integer> {

private int steps;
private StepContainer parent;

public PopulateWorker(int steps, StepContainer parent) {
this.steps = steps;
this.parent = parent;
}

@Override
protected Void doInBackground() throws Exception {
Thread.sleep(1000);
for (int index = 0; index < steps; index++) {
publish(index);
Thread.sleep(1000);
}
return null;
}

@Override
protected void process(List<Integer> chunks) {
for (int step : chunks) {
parent.addStep(step);
}
}

}

}

使用 Swing 计时器

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public interface StepContainer {

public void addStep(int index);
}

public class TestPane extends JPanel implements StepContainer {

private int step = 0;

public TestPane() {
setLayout(new GridBagLayout());
Timer timer = new Timer(1000, new PopulateAction(10, this));
timer.start();
}

@Override
public void addStep(int step) {
JButton btn = new JButton(Integer.toString(step));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

add(btn, gbc);
revalidate();
repaint();
}

}

public class PopulateAction implements ActionListener {

private int steps;
private StepContainer parent;
private int step;

public PopulateAction(int steps, StepContainer parent) {
this.steps = steps;
this.parent = parent;
}

@Override
public void actionPerformed(ActionEvent e) {
parent.addStep(step);
if (step == steps - 1) {
((Timer) e.getSource()).stop();
step = 0;
}
step++;
}
}
}

您使用哪个取决于问题的复杂程度,SwingWorker使用它自己的Thread来调用doInBackground,允许您处理时间消耗任务,但提供安全更新 UI 的能力。

Timer 在 EDT 上下文中触发其更新,使其保存以更新 UI,但不执行耗时的任务

关于java - 从 Controller 方法更新面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com