从你的代码看起来,你是阻止事件调度线程(EDT)。
EDT负责(除其他事项外)处理重新绘制事件。这意味着如果你阻止美国东部时间,什么都不能重新粉刷。
您拥有的另一个问题是,您不应该创建或修改除EDT以外的任何线程的任何UI组件。
查看Concurrency in Swing了解更多详情。
以下示例仅使用javax.swing.Timer
,而是从事物的声音,你可能会发现Swing Worker更多有用的
public class TestLabelAnimation {
public static void main(String[] args) {
new TestLabelAnimation();
}
public TestLabelAnimation() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel left;
private JLabel right;
public TestPane() {
setLayout(new BorderLayout());
left = new JLabel("0");
right = new JLabel("0");
add(left, BorderLayout.WEST);
add(right, BorderLayout.EAST);
Timer timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
left.setText(Integer.toString((int)Math.round(Math.random() * 100)));
right.setText(Integer.toString((int)Math.round(Math.random() * 100)));
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
}
我没有看到任何'JLabel'中的代码。 downvote – 2013-02-13 10:19:47
正如我所提到的,我已经尝试过Jlabel并且失败了......所以我已经清理了所有Jlabel varibales – 2013-02-13 10:22:32
那么,如果您清理了所有JLabel,那么问题是什么? – 2013-02-13 10:28:55