【发布时间】:2014-04-05 13:57:33
【问题描述】:
我的动画和 JComboBox 出现问题。在我的应用程序中,我可以使用鼠标和键盘或设置动画来移动和变换我的形状。
我的组件层次结构如下:放置在我的 JFrame 中的 JPanel 包含一个名为 EditorCanvas 的面板,用于修改和绘制形状,以及一个名为 DrawMenu 的面板,其中包含一些 JButton 和我的 JComboBox。
JFrame -> JPanel -> EditorCanvas
JFrame -> JPanel -> DrawMenu -> JComboBox
JComboBox 用于在单击时选择要添加到画布的形状。 在另一个线程中,我在计算新位置等后每 10 毫秒在画布上调用一次 repaint()...
问题是我不能再使用我的 JComboBox,因为弹出窗口在我打开后立即消失。这在某些时候是由我的重绘引起的。真正奇怪的是我的 JComboBox 被放置在另一个面板中,所以不应该重新绘制它。
我尝试在 SwingUtilities.invokeLater 调用的 Runnable 中替换我的重绘调用,但问题仍然存在。
以下是我的代码的一些相关部分:
public class EditorCanvas extends JPanel implements MouseListener, MouseMotionListener, KeyListener {
...
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// To keep keyboard focus
this.requestFocusInWindow();
this.graphics = (Graphics2D) g.create();
this.graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
this.mainRenderer.paintAll(this.graphics);
}
public class AnimationManager implements Runnable {
...
public void run () {
try {
while (Thread.currentThread() == this.thread) {
// Update time values
double curTime = System.currentTimeMillis();
double deltaTime = curTime - this.lastTime;
this.lastTime = curTime;
for (Animation anim : this.animations) {
anim.update(deltaTime);
}
SwingUtilities.invokeLater(new Runnable() {
public @Override void run() {
EditorParameters.getCanvas().repaint();
}
});
// Pause the animation if it has to
synchronized (this) {
while (this.pause) {
System.out.println("\nAnimation paused!\n");
this.wait();
}
}
Thread.sleep(SLEEP_TIME);
}
【问题讨论】:
-
尝试用
validate/revalidate/invalidate代替repaint。我不能说它是否有效,因为您没有共享完整的可测试代码。
标签: java swing animation repaint