如果在 JFrame 代码中调用 repaint(),JPanel 不会重绘
JPanel doesn't repaint if repaint() is called within JFrame code
我有一个 class Forest
和 CellularJPanel
,它们扩展 JPanel
并显示 Forest
。我写了一个原始代码来创建 JFrame
、Forest
、CellularJPanel
并将 CellularJPanel
添加到 JFrame
。接下来是一个无限循环,它使 Forest
更新并 CellularJPanel
重绘。
JFrame jFrame = new JFrame();
Forest forest = new Forest();
CellularJPanel forestJPanel = new CellularJPanel(forest);
jFrame.add(forestJPanel);
jFrame.pack();
//jFrame.setResizable(false);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
while (true)
{
try
{
forestJPanel.repaint();
forest.update();
forest.sleep(); // calls Thread.sleep(...)
}
catch (InterruptedException e)
{
}
}
这里是 CellularJPanel
class 的代码:
public class CellularJPanel extends JPanel
{
private CellularAutomata cellularAutomata;
public CellularJPanel(CellularAutomata cellularAutomata)
{
super();
this.cellularAutomata = cellularAutomata;
setPreferredSize(this.cellularAutomata.getDimension());
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D)g;
cellularAutomata.draw(graphics2D);
}
}
如果我在 main()
方法中使用上面的代码,那么一切正常,
CellularJPanel
重绘,paintComponent()
正常调用
如果我将相同的代码粘贴到 UI JFrame 按钮单击事件方法,那么新的 JFrame 会显示甚至显示 Forest
的初始状态,因为一旦调用了 paintComponent
,当调用 jFrame.setVisible(true)
时。然后while
循环正在执行,但是CellularJPanel
没有重绘,paintComponent
没有被调用。我不知道为什么,也许我应该以某种方式使用 SwingUtilities.invokeLater(...)
或 java.awt.EventQueue.invokeLater
,但我已经尝试过它们但没有用,我做错了什么。
有什么建议吗?
P.S。
我的目标是在单击按钮的同一个 UI JFrame 中显示 CellularJPanel
。但即使我将此面板添加到主 UI JFrame,它也不起作用。
有一个单独的 UI 线程来绘制东西 - 它也是处理按钮点击的线程。在 swing 中,这称为 event dispatch thread。如果 UI 线程正忙于 运行 一个 while
循环,它就无法绘制。
您可以通过使按钮单击处理程序 运行 仅循环的一次迭代(没有睡眠)来快速验证这一点:forest.update(); forestJpanel.repaint();
您可以从循环调用 repaint/sleep 的单独线程(如 Timer
)自动更新。
您的问题是 Event Dispatch Thread 上的 while(true)
会阻止与 UI 相关的任何内容,因为 UI 事件不再得到处理。
事件分派线程(单个线程)处理一个 UI 事件消息队列,直到它处理 while(true)
循环所在的那个 运行。然后它会阻止任何进一步的处理,因为它有一个无限循环。从该循环调用 SwingUtilities.invokeLater
无济于事,因为它会将事件发布到事件分派线程,该线程在 while(true)
循环中被阻塞。
所以删除那个循环,而不是使用 javax.swing.Timer
来为你的事件计时。在计时器事件中,更改 UI 的状态并调用 repaint
。计时器事件将与 UI 线程同步,因此允许更改 UI 个组件的状态。
我有一个 class Forest
和 CellularJPanel
,它们扩展 JPanel
并显示 Forest
。我写了一个原始代码来创建 JFrame
、Forest
、CellularJPanel
并将 CellularJPanel
添加到 JFrame
。接下来是一个无限循环,它使 Forest
更新并 CellularJPanel
重绘。
JFrame jFrame = new JFrame();
Forest forest = new Forest();
CellularJPanel forestJPanel = new CellularJPanel(forest);
jFrame.add(forestJPanel);
jFrame.pack();
//jFrame.setResizable(false);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
while (true)
{
try
{
forestJPanel.repaint();
forest.update();
forest.sleep(); // calls Thread.sleep(...)
}
catch (InterruptedException e)
{
}
}
这里是 CellularJPanel
class 的代码:
public class CellularJPanel extends JPanel
{
private CellularAutomata cellularAutomata;
public CellularJPanel(CellularAutomata cellularAutomata)
{
super();
this.cellularAutomata = cellularAutomata;
setPreferredSize(this.cellularAutomata.getDimension());
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D)g;
cellularAutomata.draw(graphics2D);
}
}
如果我在 main()
方法中使用上面的代码,那么一切正常,
CellularJPanel
重绘,paintComponent()
正常调用
如果我将相同的代码粘贴到 UI JFrame 按钮单击事件方法,那么新的 JFrame 会显示甚至显示 Forest
的初始状态,因为一旦调用了 paintComponent
,当调用 jFrame.setVisible(true)
时。然后while
循环正在执行,但是CellularJPanel
没有重绘,paintComponent
没有被调用。我不知道为什么,也许我应该以某种方式使用 SwingUtilities.invokeLater(...)
或 java.awt.EventQueue.invokeLater
,但我已经尝试过它们但没有用,我做错了什么。
有什么建议吗?
P.S。
我的目标是在单击按钮的同一个 UI JFrame 中显示 CellularJPanel
。但即使我将此面板添加到主 UI JFrame,它也不起作用。
有一个单独的 UI 线程来绘制东西 - 它也是处理按钮点击的线程。在 swing 中,这称为 event dispatch thread。如果 UI 线程正忙于 运行 一个 while
循环,它就无法绘制。
您可以通过使按钮单击处理程序 运行 仅循环的一次迭代(没有睡眠)来快速验证这一点:forest.update(); forestJpanel.repaint();
您可以从循环调用 repaint/sleep 的单独线程(如 Timer
)自动更新。
您的问题是 Event Dispatch Thread 上的 while(true)
会阻止与 UI 相关的任何内容,因为 UI 事件不再得到处理。
事件分派线程(单个线程)处理一个 UI 事件消息队列,直到它处理 while(true)
循环所在的那个 运行。然后它会阻止任何进一步的处理,因为它有一个无限循环。从该循环调用 SwingUtilities.invokeLater
无济于事,因为它会将事件发布到事件分派线程,该线程在 while(true)
循环中被阻塞。
所以删除那个循环,而不是使用 javax.swing.Timer
来为你的事件计时。在计时器事件中,更改 UI 的状态并调用 repaint
。计时器事件将与 UI 线程同步,因此允许更改 UI 个组件的状态。