Java Swing中有关事件机制
2015-05-12 22:10
155 查看
看到过两种方式启动主窗体的代码:
方式1:
方式2
SwingUtilities.invokeLater和EventQueue.invokeLater实际上是一样的。SwingUtilities.invokeLater实际上是调用EventQueue.invokeLater。
JDK源码
SwingUtilities.invokeLater的Javadoc有这样的一段说明:
Open Declaration void javax.swing.SwingUtilities.invokeLater(Runnable doRun)
Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread(就是EDT). This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.
If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread).
Additional documentation and examples for this method can be found in Concurrency in Swing.
As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater().
Unlike the rest of Swing, this method can be invoked from any thread.
Parameters:doRun See Also:invokeAndWait
Event Dispatching Thread就是常说的EDT,事件派发线程
关于事件分发的机制,参看下面的文章,讲的还是比较全面的。
http://blog.itpub.net/13685345/viewspace-374940/
http://blog.csdn.net/vking_wang/article/details/8992463
方式1:
java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainJFrame().setVisible(true); } });
方式2
javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MainJFrame().setVisible(true); } });
SwingUtilities.invokeLater和EventQueue.invokeLater实际上是一样的。SwingUtilities.invokeLater实际上是调用EventQueue.invokeLater。
JDK源码
public static void invokeLater(Runnable doRun) { EventQueue.invokeLater(doRun); }
SwingUtilities.invokeLater的Javadoc有这样的一段说明:
Open Declaration void javax.swing.SwingUtilities.invokeLater(Runnable doRun)
Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread(就是EDT). This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.
Runnable doHelloWorld = new Runnable() { public void run() { System.out.println("Hello World on " + Thread.currentThread()); } }; SwingUtilities.invokeLater(doHelloWorld); System.out.println("This might well be displayed before the other message.");
If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread).
Additional documentation and examples for this method can be found in Concurrency in Swing.
As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater().
Unlike the rest of Swing, this method can be invoked from any thread.
Parameters:doRun See Also:invokeAndWait
Event Dispatching Thread就是常说的EDT,事件派发线程
关于事件分发的机制,参看下面的文章,讲的还是比较全面的。
http://blog.itpub.net/13685345/viewspace-374940/
http://blog.csdn.net/vking_wang/article/details/8992463
相关文章推荐
- Java swing(awt):事件监听机制的实现原理+简单示例
- 有关Android的事件分发机制
- java SWing事件调用的两种机制
- java SWing事件调用的两种机制
- Cocos2d-x之Touch事件处理机制
- Android事件分发机制
- C#的事件处理机制应用
- Android事件分发机制浅析(3)
- jquery事件绑定解绑机制源码解析
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- [Android实例] android屏幕触摸事件机制
- java事件处理机制(自定义事件)
- SpringApplication方法run的事件监听器机制
- Touch事件的传递机制
- Android 事件传递机制应用——下拉刷新(未完成)
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- Android View事件机制 21问21答
- python 3下基于select模型的事件驱动机制程序
- flex4 的事件机制
- qt 事件机制