自IBM公司提供的跨平台GUI开发包SWT以来,越来越多受到广大程序员的亲睐,已经有不少程序员用它开发出美观、高效、实用的桌面应用程序。这让我们更有理由去探索SWT给我们带来的惊奇。
SWT在外观和性能上都超过了Swing和AWT,为什么这样说呢?下面简单的测试程序会让你一目了然。废话也不多说,让我们看Swing和AWT程序。
下面让我们写一个简单的程序来测试一下,程序只做一件事,就是用Label显示”HelloWorld!”,我的测试环境是JDK1.5.0+Eclipse3.1。看看在SWT、Swing和AWT下分别实现该效果所需要的时间和内存消耗。
AWT_CODE:
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class awtTest {
- public static void main(String[] args) {
- long memory = 0L;
- long time = 0L;
- memory = Runtime.getRuntime().freeMemory();
- time = System.currentTimeMillis();
- Frame frame = new Frame();
- Label label = new Label();
- label.setText("Hello World!");
- frame.add(label);
- frame.setVisible(true);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- frame.pack();
- System.out.println(System.currentTimeMillis() - time);
- System.out.println(memory - Runtime.getRuntime().freeMemory());
- }
- }
SWING_CODE:
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class swingTest {
- public static void main(String[] args) {
- long memory = 0L;
- long time = 0L;
- memory = Runtime.getRuntime().freeMemory();
- time = System.currentTimeMillis();
- JFrame frame = new JFrame();
- JLabel label = new JLabel();
- label.setText("Hello World!");
- frame.add(label);
- frame.setVisible(true);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- frame.pack();
- System.out.print("Time:");
- System.out.println(System.currentTimeMillis() - time);
- System.out.print("Memory:");
- System.out.println(memory - Runtime.getRuntime().freeMemory());
- }
- }
SWT_CODE:
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.SWT;
- public class swtTest {
- public static void main(String[] args) {
- long memory = 0L;
- long time = 0L;
- memory = Runtime.getRuntime().freeMemory();
- time = System.currentTimeMillis();
- Display display = new Display();
- Shell shell = new Shell(display);
- Label label = new Label(shell, SWT.NONE);
- label.setText("Hello World!");
- shell.pack();
- label.pack();
- shell.open();
- System.out.print("Time:");
- System.out.println(System.currentTimeMillis() - time);
- System.out.print("Memory:");
- System.out.println(Runtime.getRuntime().freeMemory() - memory);
- while(!shell.isDisposed()) {
- if(!display.readAndDispatch()) {
- display.sleep();
- }
- }
- display.dispose();
- label.dispose();
- }
- }
【编辑推荐】