DNB*_*ims 2 java swing event-dispatch-thread
来自java.sun的简单代码:
public class BasicApp implements Runnable {
JFrame mainFrame;
JLabel label;
public void run() {
mainFrame = new JFrame("BasicApp");
label = new JLabel("Hello, world!");
label.setFont(new Font("SansSerif", Font.PLAIN, 22));
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
mainFrame.setVisible(false);
// Perform any other operations you might need
// before exit.
System.exit(0);
}
});
mainFrame.add(label);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
Runnable app = new BasicApp();
try {
SwingUtilities.invokeAndWait(app);
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以把所有这个方法都放到main()中,但为什么我需要一个单独的run方法来实现runnable来执行呢?这个概念背后的想法是什么?谢谢.
归档时间: |
|
查看次数: |
170 次 |
最近记录: |