SWT比價(jià)Swing和AWT
自IBM公司提供的跨平臺(tái)GUI開(kāi)發(fā)包SWT以來(lái),越來(lái)越多受到廣大程序員的親睞,已經(jīng)有不少程序員用它開(kāi)發(fā)出美觀、高效、實(shí)用的桌面應(yīng)用程序。這讓我們更有理由去探索SWT給我們帶來(lái)的驚奇。
SWT在外觀和性能上都超過(guò)了Swing和AWT,為什么這樣說(shuō)呢?下面簡(jiǎn)單的測(cè)試程序會(huì)讓你一目了然。廢話也不多說(shuō),讓我們看Swing和AWT程序。
下面讓我們寫一個(gè)簡(jiǎn)單的程序來(lái)測(cè)試一下,程序只做一件事,就是用Label顯示”HelloWorld!”,我的測(cè)試環(huán)境是JDK1.5.0+Eclipse3.1??纯丛赟WT、Swing和AWT下分別實(shí)現(xiàn)該效果所需要的時(shí)間和內(nèi)存消耗。
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();
- }
- }
【編輯推薦】