Kimi's NutShell

我荒废的今日,正是昨日殒身之人祈求的明日

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

 

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

 

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

 

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();

       }

}

运行效果及所用的时间和内存值:

 

如果你仔细看代码,你会发现在 SWT 的代码中,我注解为 ’***’ 代码处,与前两者有所不同,这也我疑惑的地方,在整个程序运行中,运行后的剩余内存居然比运行前的剩余内存值大。接着,我在调试该程序的时候,我发现在 Shell shell = new Shell(display) 语句执行后,内存值有明显的增加,如果你知道 SWT 底层是如何操作的,知道这是什么原因引起的,请你 联系我 并告诉我这是为什么,我好做出修正。

 

如果你是初学者,不知道怎么运行 SWT 程序,其实很简单的,只需要在你的工程的 Libraries 添加一个名为 org.eclipse.swt.win32.win32.x86_3.1.0.jar 包,该包的位置在你的 Eclipse 的安装目录下的 \plugins\ 文件夹里。

posted on 2007-02-02 12:46 Kimi 阅读(624) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: