新西兰服务器

SWT、Swing和AWT有什么区别


SWT、Swing和AWT有什么区别

发布时间:2021-12-28 09:22:44 来源:高防服务器网 阅读:77 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关SWT、Swing和AWT有什么区别,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

关于“SWT、Swing和AWT有什么区别”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[