1.SWTåSwing代ç çåºå«
SWTåSwing代ç çåºå«
ããç¬è è¿å»å¦ä¹ è¿Swingï¼ä¹ç®æ¯AWTçå¢å¼ºçå§ï¼ ç°å¨å¼å§å¦ä¹ ä¸ä¸ SWT è¿æ²¡æå¼å§æ·±å ¥ JFace å RCP ä¸æ¥æ¥æ¥å§ å å¦å¥½åºç¡ç åä¸ç¨çæ¥å项ç®ç¨ æ¬æ讨论çå å®¹ä» éäºæå·²ç¥ç SWTåSwing èå´ å¸æè½å¯¹æ Swing ç»éªç人快éäºè§£ SWT ææ帮å©ãã第ä¸ä¸ªä¸åç¹å°±æ¯ SWT çç±»åºç»æä¸å Swing 没æåä¸çç¶ç»ä»¶ SWT ä¸æ好å 个ç¶ç±»
ããâWidget ââ åºæ¬ç SWT GUI ç»ä»¶ï¼ç±»ä¼¼äº Java AWT ä¸ç Component å Swing ä¸ç JComponentï¼ Widget æ¯ä¸ä¸ªæ½è±¡ç±»
ããâControl ââ æ¥ææä½ç³»ç»ç对çç©ççªå£å°é¨ä»¶ï¼æ¢å¥è¯è¯´ å¨æä½ç³»ç»ä¸å ·æåä¸èº«ä»½ï¼ Control æ¯ä¸ä¸ªæ½è±¡ç±»
ããâComposite ââ å å«å ¶ä»æ§ä»¶çæ§ä»¶ï¼ç±»ä¼¼äº Java AWT ä¸ç Container å Swing ä¸ç JPanelï¼
ããâItem ââ å ¶ä»æ§ä»¶å å«ççªå£å°é¨ä»¶ï¼è¯¥æ§ä»¶å¯è½ä¸æ¯å¤åæ§ä»¶ï¼ æ¯å¦å表å表 注æ å å«ä¸äºé¡¹çæ§ä»¶å¾å°å å«å ¶ä»æ§ä»¶ åä¹äº¦ç¶ Item æ¯ä¸ä¸ªæ½è±¡ç±»
ããSWT çç»ä»¶ä¸è½éæç»§æ¿ å ¶æºä»£ç ä¸ä¼æ 注 IMPORTANT This 推荐系统 实现源码class is <em>not</em> intended to be subclassed å¦æä½ é£æ ·åäº è¿è¡çæ¶å SWT ç³»ç»å°±ä¼æ¥é ä¹ä¸è½ä»»æå å«åç»ä»¶ æäºç»ä»¶ ä¾å¦ Tree åªè½æ¥æ¶ TreeItem ä½ä¸ºå ¶åç»ä»¶ è¿æ ·å¨åä¸äºå¯éç¨çå¾å½¢ç»ä»¶çæ¶åå¿ é¡»æ³¨æ Swing ä¸çåæ³ä¸è½ç´æ¥å¥ç¨å¨ SWT ä¸
ããSwing çææå¾å½¢çé¢ç±»é½ç»§æ¿èª java awt Container è¿æå³çææç Swing ç»ä»¶é½å¯ä»¥å å«ä»»æçåç»ä»¶ ä¹å¯ä»¥è¢«ç»§æ¿ è¿æ¯ä¸¤ä¸ªç±»åºç第ä¸ä¸ªä¸åç¹
ãã第äºä¸ªä¸åç¹å°±æ¯ SWT 没æå©ç¨ Java çèªå¸¦åå¾æ¶éåè½æ¥ç®¡ç对象çéæ¯ å¤§é¨å对象é½å¿ é¡»è°ç¨ disposeï¼ï¼ æ¹å¯å®æèµæºçéæ¾
ãã第ä¸ä¸ªä¸åç¹å°±æ¯å¨ç»ä»¶çæ¾ç¤ºä¸ SWT æ½è±¡äºä¸ä¸ª Display 对象æ¥å¤ç SWT åæä½ç³»ç»åºå±ç»ä»¶çéä¿¡ èµæºç³è¯·éæ¾ äºä»¶å¾ªç¯ ä»»ä½æ¾ç¤ºç SWT ç»ä»¶å¿ é¡»æä¸ä» æä¸ä¸ª Display 对象
ãã第å个ä¸åç¹å°±æ¯ Swing çç»ä»¶å®ç°äº MVC æºå¶ ä½æ¯ SWT 没æï¼ç®åæ¥çï¼æä¾ MVC æºå¶ ä¹å°±æ¯è¯´ç»å¤§å¤æ° SWT ç»ä»¶é½æ¯ä¸å¯ä»¥æ´æ¢æ¾ç¤ºçå¤è§ç æçæææ¯ä¾å¦åç®è¤è¿æ ·çæºå¶ ä¾å¦ å®ç°ä¸ä¸ª Office å¤è§çæ©è²èåæ¡çæ¾ç¤º SWT æ¯ä¸è½éè¿ç®åçç¼ç æ¥å®ç°çï¼ä¹è®¸æ ä½æ¯è¿æ²¡æ¾å°ç¸å ³çèµæï¼
ããSWT çæ¯ä¸ªç»ä»¶å¯¹è±¡æä¾äº setDataï¼Objectï¼ è¿æ ·çæ¹æ³æ¥ç»ç»ä»¶åå¨ä¸ä¸ªæè å¤ä¸ªæ°æ®å¯¹è±¡ Swing ä¸æ²¡æ类似çæ¦å¿µ
ãã第äºä¸ªä¸åç¹å°±æ¯ç»å¤§å¤æ°ç SWT ç»ä»¶å建çæ¶åé½å¿ é¡»æå®ç¶ç»ä»¶åé£æ ¼å±æ§ ä¹å°±æ¯å¿ é¡»æä¸ä¸ª parent 对象 ä¹ä¸è½è¿è¡çæ¶åå¨æåæ´ç¶ç»ä»¶ å ·ä½ä¾å¦ Menu 对象éæ Decorations getParent ï¼ï¼ ä½æ¯å¹¶æ 对åºç setParentï¼ï¼ æ¹æ³ ç»ä»¶å建ä¹åå³èªå¨å å ¥ç¶ç»ä»¶æä¸ºå ¶ä¸å è¿å Swing çå¿ é¡»éè¿ addï¼ï¼ æ¹æ³æ¥å å ¥ç¶ç»ä»¶æ¯ä¸ä¸æ ·ç 大å¤æ°ç SWT ç»ä»¶ä¸æä¾æ åæ°çæé å¨ è¿å°±æå³è¿ä»ä»¬ä¸æ¯ JavaBean è¿ç¹ä¸ Swing çç»å¤§å¤æ°ç»ä»¶é½æ¯ JavaBean
ããæ»ç»ä»¥ä¸è¿äºå 容æ们å¯ä»¥æè§å° SWT æ´åæ¯æä½ç³»ç»æ¬å°ç»ä»¶çä¸ç§å°è£ èä¸æ¯åºäº Java OO çæ¦å¿µå¯¹å ¶è¿è¡æ å° å ¶ä¼ç¹å°±æ¯æ°æ®ç»æç®å
ããå¥½äº æ¥ä¸æ¥è¿æ¯éè¿ä¸äºSWTåSwingç代ç æ¥æ¯è¾å§ 第ä¸ä¸ªæ¯ç»å ¸ç Hello World
ããSWT çæ¬
ãã
ããimport eclipse swt widgets *;
ããpublic class SWTHelloWorld {
ããpublic static void main(String[] args) { Display display = new Display();Shell shell = new Shell(display);shell setText( Hello World! );shell open();while (!shell isDisposed()) { if (!display readAndDispatch())display sleep();}display dispose();}}
ããSwing çæ¬
ãã
ããimport javax swing JFrame;
ããpublic class SwingHelloWorld {
ããpublic static void main(String[] args) { JFrame frame = new JFrame();frame setTitle( Hello World! );frame setDefaultCloseOperation(JFrame DISPOSE_ON_CLOSE);frame setSize( );frame setVisible(true);}}
ããSWT ç Text å±æ§å¨ä¸åçç»ä»¶ä¸æä¸åçææ 对äºçªä½æ¥è¯´å®æ¯æ é¢ å¯¹äºæ§ä»¶æ¥è¯´å®æ¯æ¾ç¤ºçææ¬ Swing ä¸è¿ä¸ªæ¯åå¼ç 对äºçªå£æ¥è¯´æ¯ Title å±æ§ 对äºæ§ä»¶æ¥è¯´æ¯ Text å±æ§ SWT çäºä»¶å¾ªç¯åºäºæ¾å¼ç while æ¨¡å¼ è Swing å·²ç»å¨åºå±å°è£ 好äºäºä»¶ç循ç¯åå¤ç æ以è¿ä¸¤æ®µä»£ç æææ¾çä¸åå°±æ¯ SWT å¤äºæ¶é´å¾ªç¯çé¨å å³æä¸ç²ä½çé¨å
ããæ¥çæ们æ¥ççç»§æ¿ SWT çç»ä»¶ä¼æä»ä¹åæ å¨ Swing ä¸ç»§æ¿ä¸ä¸ªç»ä»¶ç¶ååå ¶ä¸æ·»å åç»ä»¶ ä¾å¦å»ºç«ä¸ä¸ªèªå®ä¹çå·¥å ·æ ç¶ååå ¶ä¸å å ¥åæ ç® å¹¶æåæ¾ç¤ºåºæ¥ è¿æ¯ç»å¸¸è¿è¡çæä½ åçç SWT ä¸
ãã
ããimport eclipse swt widgets Display;import eclipse swt widgets Shell;import eclipse swt widgets ToolItem;import eclipse swt SWT;
ããpublic class NewToolBar extends eclipse swt widgets ToolBar {
ããpublic static void main(String[] args) { try { Display display = Display getDefault();Shell shell = new Shell(display);new NewToolBar(shell SWT NULL);
ããshell open();while (!shell isDisposed()) { if (!display readAndDispatch())display sleep();}} catch (Exception e) { e printStackTrace();}}
ããpublic NewToolBar(Shell parent int style) { super(parent style);new ToolItem(this SWT NULL);}
ãã}
lishixinzhi/Article/program/Java/hx//