public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MyDialog dialog = new MyDialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
MyDialog 类只有很少的组合和文本字段,并且正在使用 DB 值填充组合。在选择一个组合值时,我从数据库中获取另一个值以填充下一个组合。
上述程序在不使用 invokeLater 线程的情况下以相同的方式运行。 invokeLater 何时在 Swing 编程中变得有用。我已经阅读了一些相关内容,但似乎都是理论上的。 invokeLater 对应用程序有什么不同?仅在 main 方法中使用它就足够了吗?还是应该在 action Listeners 中使用它?
SwingUtilities.invokeLater 和 java.awt.EventQueue.invokeLater - 它们相同吗?
最佳答案
没有关于它的理论。非常实用。 SwingUtilities.invokeLater()
方法保证 Runnable
中的代码将在 Event Dispatch Thread (EDT)
上运行。这很重要,因为 Swing 不是线程安全的,因此与 GUI 相关的任何东西(Swing
等)都需要在 EDT
上运行。 EDT
是一个“它发生时就会发生”的线程,它不保证事情的执行顺序。如果 GUI 代码在后台线程中执行(例如,在 SwingWorker
实例中),那么它可能会抛出错误。我通过艰难的方式了解到这一点:在我的学习岁月中,在后台线程中执行 GUI 更改代码会导致我无法弄清楚的随机、不一致的 RuntimeException
。这是一次很好的学习体验(SwingWorker
有一个用于后台任务的 doInBackground()
方法和一个用于 EDT< 的
任务)。done()
方法
就像您不想在后台线程上执行 GUI 代码一样,您也不希望在 EDT
上执行大型操作(数据库查询等)。这是因为 EDT
正在分派(dispatch)所有 GUI 事件,因此 EDT
上的所有内容都应该非常简短。您可以通过将 JProgressBar
设置为 indeterminate 来轻松地看到这一点。
这个 SSCCE 应该很好地说明了这一点。请注意调用 method()
时 JProgressBar
的运动,一次在后台线程上,一次在 EDT
线程上。
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
/**
*
* @author Ryan
*/
public class Test {
public static void main(String args[]) {
JFrame frame = new JFrame();
JProgressBar jpb = new JProgressBar();
jpb.setIndeterminate(true);
frame.add(jpb);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Task().execute();
}
public static void method() { // This is a method that does a time-consuming task.
for(int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
static class Task extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
/* Executing method on background thread.
* The loading bar should keep moving because, although method() is time consuming, we are on a background thread.
*/
method();
return null;
}
@Override
protected void done() {
/* Executing method on Event Dispatch Thread.
* The loading bar should stop because method() is time consuming and everything on the Event Dispatch Thread
* (like the motion of the progress bar) is waiting for it to finish.
*/
//
method();
}
}
}
希望这对您有所帮助。
关于java - Swing 线程安全编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18311972/