SWT比价Swing和AWT

自IBM公司提供的跨平台GUI开发包SWT以来,越来越多受到广大程序员的亲睐,已经有不少程序员用它开发出美观、高效、实用的桌面应用程序。这让我们更有理由去探索SWT给我们带来的惊奇。

SWT在外观和性能上都超过了Swing和AWT,为什么这样说呢?下面简单的测试程序会让你一目了然。废话也不多说,让我们看Swing和AWT程序。

下面让我们写一个简单的程序来测试一下,程序只做一件事,就是用Label显示”HelloWorld!”,我的测试环境是JDK1.5.0+Eclipse3.1。看看在SWT、Swing和AWT下分别实现该效果所需要的时间和内存消耗。

AWT_CODE:

 
 
 
  1. import java.awt.Frame;  
  2. import java.awt.Label;  
  3. import java.awt.event.WindowAdapter;  
  4. import java.awt.event.WindowEvent;  
  5.  
  6. public class awtTest {  
  7. public static void main(String[] args) {  
  8. long memory = 0L;  
  9. long time = 0L;  
  10. memory = Runtime.getRuntime().freeMemory();  
  11. time = System.currentTimeMillis();  
  12. Frame frame = new Frame();  
  13. Label label = new Label();  
  14. label.setText("Hello World!");  
  15. frame.add(label);  
  16. frame.setVisible(true);  
  17. frame.addWindowListener(new WindowAdapter() {  
  18. public void windowClosing(WindowEvent we) {  
  19. System.exit(0);  
  20. }  
  21. });  
  22. frame.pack();  
  23. System.out.println(System.currentTimeMillis() - time);  
  24. System.out.println(memory - Runtime.getRuntime().freeMemory());  
  25. }  

SWING_CODE:

 
 
 
  1. import javax.swing.JFrame;  
  2. import javax.swing.JLabel;  
  3. import java.awt.event.WindowAdapter;  
  4. import java.awt.event.WindowEvent;  
  5.  
  6. public class swingTest {  
  7. public static void main(String[] args) {  
  8. long memory = 0L;  
  9. long time = 0L;  
  10. memory = Runtime.getRuntime().freeMemory();  
  11. time = System.currentTimeMillis();  
  12. JFrame frame = new JFrame();  
  13. JLabel label = new JLabel();  
  14. label.setText("Hello World!");  
  15. frame.add(label);  
  16. frame.setVisible(true);  
  17. frame.addWindowListener(new WindowAdapter() {  
  18. public void windowClosing(WindowEvent we) {  
  19. System.exit(0);  
  20. }  
  21. });  
  22. frame.pack();  
  23. System.out.print("Time:");  
  24. System.out.println(System.currentTimeMillis() - time);  
  25. System.out.print("Memory:");  
  26. System.out.println(memory - Runtime.getRuntime().freeMemory());  
  27. }  

SWT_CODE:

 
 
 
  1. import org.eclipse.swt.widgets.Display;  
  2. import org.eclipse.swt.widgets.Shell;  
  3. import org.eclipse.swt.widgets.Label;  
  4. import org.eclipse.swt.SWT;  
  5.  
  6. public class swtTest {  
  7. public static void main(String[] args) {  
  8. long memory = 0L;  
  9. long time = 0L;  
  10. memory = Runtime.getRuntime().freeMemory();  
  11. time = System.currentTimeMillis();  
  12. Display display = new Display();  
  13. Shell shell = new Shell(display);  
  14. Label label = new Label(shell, SWT.NONE);  
  15. label.setText("Hello World!");  
  16. shell.pack();  
  17. label.pack();  
  18. shell.open();  
  19. System.out.print("Time:");  
  20. System.out.println(System.currentTimeMillis() - time);  
  21. System.out.print("Memory:");  
  22. System.out.println(Runtime.getRuntime().freeMemory() - memory);  
  23. while(!shell.isDisposed()) {  
  24. if(!display.readAndDispatch()) {  
  25. display.sleep();  
  26. }  
  27. }  
  28. display.dispose();  
  29. label.dispose();  
  30. }  

【编辑推荐】

  1. 在表格中Swing增加列表框
  2. 浅谈Swing控件JList
  3. 概述Swing组件与外部线程
  4. Java Swing做什么好
  5. Swing文件选择器的制作
THE END