![](/img/trans.png)
[英]Java repaint() method doesn't work when I try to delete shapes from panel
[英]Java awt Panel doesn't repaint from thread and only updates when I resize the window
我正在嘗試從線程將標簽添加到 awt 面板,並從線程調用重繪,但面板沒有更新,並且僅在我調整窗口大小時更新。 我嘗試了很多解決方案,但沒有一個有效,所以請幫忙。
public class Updater extends Thread{
private Panel p;
public Updater(Panel p) {
this.p = p;
}
public void run() {
while (true) {
p.add(new Label("Text"), BorderLayout.NORTH);
p.repaint();
System.out.println("Dodao");
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
public class Main extends Frame {
public Panel p;
private void populateWindow() {
p = new Panel();
p.setLayout(new GridLayout(0, 1));
add(p);
}
public Main() {
setSize(1000, 500);
setResizable(true);
setTitle("Test");
populateWindow();
addWindowListener(new WindowAdapter () {
public void windowClosing(WindowEvent e) {
dispose();
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
Updater u = new Updater(m.p);
u.start();
}
}
使用revalidate()
而不是repaint()
。 Revalidate()
用於組件, repaint()
不是。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.