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线程,上述程序将以相同的方式运行。在Swing编程中,何时使用invokeLater会变得有用?我已经读了一些关于它的理论知识,但似乎都是理论性的。invokeLater对应用程序有什么区别?仅在main方法内部使用它是否足够,还是也应在操作监听器中使用它?
SwingUtilities.invokeLater和java.awt.EventQueue.invokeLater - 它们是相同的吗?
SwingUtilities.invokeLater()
调用了java.awt.EventQueue.invokeLater()
。 - kiheruSwingUtilities.isEventDispatchThread()
来检查此内容。必须始终注意,就像在此示例中所做的那样,在displaySelectionInfo(...)
方法内部进行查看,并请阅读相同位置上方的注释。希望这给您留下了关于整个事情的大致印象 :-) - nIcE cOw