国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

invokeLater和invokeAndWait

2019-11-06 06:07:19
字體:
供稿:網(wǎng)友

invokeLater和invokeAndWait的區(qū)別

invokeLater:在把可運行的對象放入隊列后就返回,而invokeAndWait一直等待已啟動了可運行的run方法才返回。如果一個操作在另外一個操作執(zhí)行前必須從一個組件獲得信息,則invokeAndWait方法很有用的。

public class SwingDemoInvokeAndWait { public static void main(String[] argv) throws InterruptedException, InvocationTargetException { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { constructUI(); } }); final Runnable doHelloWorld = new Runnable() { public void run() { System.out.PPThread = new Thread() { public void run() { try { SwingUtilities.invokeAndWait(doHelloWorld); } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished on " + Thread.currentThread()); } }; appThread.start(); } private static void constructUI() { JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER); JFrame frame = new JFrame("Bulletin"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(bulletin); frame.setSize(200, 150); frame.setVisible(true); bulletin.setForeground(Color.RED); } }

由于doHelloWorld是在invokeAndWait中被執(zhí)行的,所以 一定會等待doHelloWorld方法的執(zhí)行并返回,即”Hello World on”一定會在”Finished on”前顯示出來。

import java.awt.Color; import java.lang.reflect.InvocationTargetException; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class SwingDemoInvokeLater { public static void main(String[] argv) throws InterruptedException, InvocationTargetException { final Runnable doHelloWorld = new Runnable() { public void run() { System.out.println("Hello World on " + Thread.currentThread()); } }; Thread appThread = new Thread() { public void run() { try { SwingUtilities.invokeLater(doHelloWorld); } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished on " + Thread.currentThread()+",but this might well be displayed before the other message."); } }; appThread.start(); } private static void constructUI() { JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER); JFrame frame = new JFrame("Bulletin"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(bulletin); frame.setSize(200, 150); frame.setVisible(true); bulletin.setForeground(Color.RED); } }

由于doHelloWorld是在invokeLater中被執(zhí)行的,因而“Finished on”有可能出現(xiàn)在其他信息的前面比如”Hello World On”。

參考文章:http://developer.51cto.com/art/201201/313034.htm http://blog.csdn.net/legendmohenote/article/details/5853833 http://blog.csdn.net/yanwushu/article/details/39434159


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 富裕县| 铜陵市| 景谷| 高青县| 青龙| 九龙坡区| 浪卡子县| 九寨沟县| 大城县| 朝阳县| 鄂伦春自治旗| 温泉县| 集贤县| 武邑县| 红原县| 泾源县| 富蕴县| 凯里市| 双柏县| 疏勒县| 乌恰县| 宁德市| 临清市| 定南县| 监利县| 池州市| 高邑县| 岑溪市| 法库县| 伊川县| 微博| 厦门市| 汶川县| 城市| 城固县| 扎囊县| 永嘉县| 云南省| 嘉定区| 桦甸市| 玉门市|