【发布时间】:2014-01-08 10:26:29
【问题描述】:
是否可以从另一个对象的循环中重新绘制 JPanel?我有一个由 JPanel (DrawPanel) 和 SA 对象组成的 JFrame。我想在这个 SA 对象的 while 循环期间更新/重绘 JPanel。我启动了一个新线程,但 panel.repaint() 仍然没有执行。
public class Mainform extends JFrame {
private DrawPanel DrawPanel;
public static void main(String[] args) {
DrawPanel panel = new DrawPanel();
SA sa = new SA(panel);
Thread t = new Thread(sa);
t.start();
//...
}
}
public class DrawPanel extends JPanel implements MouseMotionListener, MouseListener {
public DrawPanel() {
super();
setBackground(Color.WHITE);
addMouseWheelListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
//...
}
public class SA implements Runnable {
private DrawPanel panel;
public SA(DrawPanel p) {
this.panel = p;
init();
}
public void run() {
while (true) {
//...
panel.repaint();
}
}
}
编辑:运行是公开的
【问题讨论】:
-
run 不能是私有的 - 但基本上,是的......这假设您尝试重绘的面板已添加到容器中,该容器附加到本机对等点并且是可见的。 ..
-
尝试使面板无效,而不是调用 repaint()。
-
@jtmon 使组件无效将标记布局,一旦布局,将重新绘制...