【发布时间】:2017-01-24 00:35:55
【问题描述】:
我想使用 SwingUtilities.invokeLater 方法,以便我的程序的所有 Swing 组件都由 事件调度线程 更新,因为这是一个好习惯。
但是,如果我将 main 方法的所有代码包装在 SwingUtilities.invokeLater(new Runnable { public void run() { /* code */ }}); 中,窗口就会冻结(这是正常的,因为我的代码有一个动画循环,需要几秒钟才能完成)。我应该把SwingUtilities.invokeLater 方法放在哪里?
程序代码(没有SwingUtilities.invokeLater 方法)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
int width = 854;
int height = 480;
String title = "Test";
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.drawImage(bufferedImage, 0, 0, null);
}
};
frame.add(panel);
frame.getContentPane().setPreferredSize(new Dimension(width, height));
frame.pack();
frame.setTitle(title);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
int size = height/3;
int x = -size;
while (x <= width) {
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
graphics2D.setColor(Color.RED);
graphics2D.fill(new Rectangle2D.Double(x, 0, size, size));
graphics2D.setColor(Color.GREEN);
graphics2D.fill(new Rectangle2D.Double(x, size, size, size));
graphics2D.setColor(Color.BLUE);
graphics2D.fill(new Rectangle2D.Double(x, 2 * size, size, size));
graphics2D.dispose();
panel.repaint();
++x;
try {
Thread.sleep(10);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
frame.dispose();
}
}
程序截图
这是一个动画,其中三个彩色条逐渐延伸到窗口的右边缘。
【问题讨论】:
-
您
while-loop正在阻止 EDT,阻止更新。 SwingTimer可能是您正在寻找的解决方案。对于初学者,请查看How to use Swing Timers。您需要将计时器视为一个伪循环,每次计时,它都充当循环的迭代。因为它在 EDT 的上下文中更新,所以可以安全地用于更新 UI -
您的主要静态方法中有太多代码。实际上,您的程序不过是一个大的静态 main 方法。相反,您的代码应该努力更加符合 OOP,并且您的 main 方法应该只用于创建主类、将它们连接在一起、启动它们运行,仅此而已。
-
动画,嗯……哦,你听说过 JavaFX 吗?
-
Swing 是为动画非常繁重的旧系统设计的。现在有了更强的 CPU 能力,加上硬件加速,Swing 有点过时了。 JavaFX 让动画的使用变得轻而易举。
标签: java swing animation event-dispatch-thread invokelater