考虑这两个类,你能解释一下它们之间的区别吗?我知道第一个类创建了一个类的对象,但这不是我关心的...... 具体来说,代码块包括以下行, **javas.swing.swingUtiliies.invokeLater(new Runnable() {
//也都导入javax.swing
public class HelloWorld extends JFrame{
public HelloWorld(){
super("HelloWorldSwing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
getContentPane().add(label);
pack();
setVisible(true);
}
public static void main(String[] args) {
HelloWorld h = new HelloWorld();
}
}
public class HelloWorldSwing {
private static void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
最佳答案
第二个例子坚持在事件调度线程上执行所有与 GUI 相关的代码的规则,这是通过将带有所述代码的 Runnable
传递给 invokeLater
方法来实现的.
我所说的“GUI 相关代码”是指实例化任何 AWT/Swing 类,调用它们的任何方法或访问任何属性。
关于java - ?关于运行 GUI 类..请协助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18089260/