java

位置:IT落伍者 >> java >> 浏览文章

基于Eclipse 3.0的SWT编程


发布日期:2020年02月23日
 
基于Eclipse 3.0的SWT编程
SWT简介

Java语言的声望和它在桌面应用程序(GUI程序)所取得的成就显然极不相符至今仍然很少能看到非常成功Java桌面程序虽然有JBuilderNetbeanJProbe等大型软件作为代表但这仍不能证明Java的GUI程序是成功的它们的外观总是和同一操作系统平台下的其它软件显得格格不入对机器配置的需求也似乎永无止境这使得它们只能被一些总是拥有当前最高性能PC的程序员们所容忍或是那些不在乎金钱和时间的专业用户所接受对绝大多数计算机使用者来说AWT或SWING代表着怪异的界面和无法接受的速度Standard Widget Toolkit(SWT)或许是Java这一噩梦的终结者广大Java程序员终于可以开发出高效率的GUI程序它们拥有标准的外观几乎没有人能看出你的程序是用Java写出来的更为重要的是这些程序是跨平台的

SWT本身仅仅是Eclipse组织为了开发Eclipse IDE环境所编写的一组底层图形界面 API或许是无心插柳或是有意为之至今为止SWT无论是在性能和外观上都超越了SUN公司提供的AWT和SWING目前Eclipse IDE已经开发到了版本SWT已经十分稳定这里指的稳定应该包含两层意思

一是指性能上的稳定其中的关键是源于SWT的设计理念SWT最大化了操作系统的图形构件API就是说只要操作系统提供了相应图形的构件那么SWT只是简单应用JNI技术调用它们只有那些操作系统中不提供的构件SWT才自己去做一个模拟的实现可以看出SWT的性能上的稳定大多时候取决于相应操作系统图形构件的稳定性

另一个稳定是指SWT API包中的类方法的名称和结构已经少有改变程序员不用担心由于Eclipse组织开发进度很快(Eclipse IDE每天都会有一个Nightly版本的发布)而导致自己的程序代码变化过大从一个版本的SWT更新至另一版本通常只需要简单将SWT包换掉就可以了

Eclipse的SWT编程

SWT比AWT和Swing要快多因为它是利用操作系统的界面组件生成UI的在java桌面设计领域掀起一场革命

环境配置

windows系统+eclipse

具体操作

()新建一java项目命名SWT文件结构如下

+swt

+bin(编译输出)

+src(原文件)

+AddressBookUIjava

+swtawtwindll(以下均从eclipse\plugins\orgeclipseswtwin_\os\win\x下导入)

+swtwindll

+javawexemanifest

()到项目的properties里在java build path | libraries里将swtjar导入

()AddressBookUIjava原代码如下

import orgeclipseswtwidgetsDisplay;

import orgeclipseswtwidgetsShell;

import orgeclipseswtSWT;

import orgeclipseswtwidgetsButton;

import orgeclipseswtwidgetsGroup;

import orgeclipseswtwidgetsLabel;

import orgeclipseswtwidgetsText;

import orgeclipseswtwidgets*;

import orgeclipseswteventsSelectionAdapter;

import orgeclipseswteventsSelectionEvent;

public class AddressBookUI {

private Shell shell;

private Text miscText;

private Text addrText;

private Text emailText;

private Text phoneText;

private Text lnameText;

private Text fnameText;

private Button cancelButton;

private Button saveButton;

private Button nextButton;

private Button prevButton;

private Button editButton;

private Button deleteButton;

private Button newButton;

public static void main(String[] args) {

AddressBookUI window = new AddressBookUI();

windowopen();

}

public void open() {

final Display display = new Display();

shell = new Shell();

shellsetSize( );

shellsetText(Address Book);

{

newButton = new Button(shell SWTNONE);

newButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

clearText();

setTextEditable(true);

enableEditButtons(false);

enableSaveButtons(true);

Systemoutprintln(New button selected);

}

});

newButtonsetBounds( );

newButtonsetText(New);

}

{

deleteButton = new Button(shell SWTNONE);

deleteButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

clearText();

Systemoutprintln(Delete button selected);

}

});

deleteButtonsetBounds( );

deleteButtonsetText(Delete);

}

{

editButton = new Button(shell SWTNONE);

editButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

setTextEditable(true);

enableEditButtons(false);

enableSaveButtons(true);

Systemoutprintln(Edit button selected);

}

});

editButtonsetBounds( );

editButtonsetText(Edit);

}

{

prevButton = new Button(shell SWTNONE);

prevButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

Systemoutprintln(Previous button selected);

}

});

prevButtonsetBounds( );

prevButtonsetText(Previous);

}

{

nextButton = new Button(shell SWTNONE);

nextButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

Systemoutprintln(Next button selected);

}

});

nextButtonsetBounds( );

nextButtonsetText(Next);

}

{

saveButton = new Button(shell SWTNONE);

saveButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

setTextEditable(false);

enableEditButtons(true);

enableSaveButtons(false);

Systemoutprintln(Save button selected);

}

});

saveButtonsetBounds( );

saveButtonsetText(Save);

saveButtonsetEnabled(false);

}

{

cancelButton = new Button(shell SWTNONE);

cancelButtonaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

setTextEditable(false);

enableEditButtons(true);

enableSaveButtons(false);

Systemoutprintln(Cancel button selected);

}

});

cancelButtonsetBounds( );

cancelButtonsetText(Cancel);

cancelButtonsetEnabled(false);

}

{

final Group group = new Group(shell SWTNONE);

groupsetText(Details);

groupsetBounds( );

{

final Label label = new Label(group SWTNONE);

labelsetBounds( );

labelsetText(First Name:);

}

{

final Label label = new Label(group SWTNONE);

labelsetBounds( );

labelsetText(Last Name:);

}

{

final Label label = new Label(group SWTNONE);

labelsetBounds( );

labelsetText(Phone:);

}

{

final Label label = new Label(group SWTNONE);

labelsetBounds( );

labelsetText(Email:);

}

{

final Label label = new Label(group SWTNONE);

labelsetBounds( );

labelsetText(Address:);

}

{

final Label label = new Label(group SWTNONE);

labelsetBounds( );

labelsetText(Miscellaneous Information:);

}

{

fnameText = new Text(group SWTBORDER | SWTREAD_ONLY);

fnameTextsetBounds( );

}

{

lnameText = new Text(group SWTBORDER | SWTREAD_ONLY);

lnameTextsetBounds( );

}

{

phoneText = new Text(group SWTBORDER | SWTREAD_ONLY);

phoneTextsetBounds( );

}

{

emailText = new Text(group SWTBORDER | SWTREAD_ONLY);

emailTextsetBounds( );

}

{

addrText = new Text(group SWTBORDER | SWTREAD_ONLY | SWTV_SCROLL);

addrTextsetBounds( );

}

{

miscText = new Text(group SWTBORDER | SWTREAD_ONLY | SWTV_SCROLL);

miscTextsetBounds( );

}

}

setupMenu();

shellopen();

while (!shellisDisposed()) {

if (!displayreadAndDispatch())

displaysleep();

}

}

private void setupMenu() {

//create the menu bar

Menu menu = new Menu(shell SWTBAR);

shellsetMenuBar(menu);

//add the File option to it

MenuItem file = new MenuItem(menu SWTCASCADE);

filesetText(File);

//create a menu for the File option

filesetMenu(new Menu(file));

//add MenuItems to the File menu

MenuItem prevItem = new MenuItem(new Menu(file) SWTNONE);

prevItemsetText(Previous);

MenuItem nextItem = new MenuItem(new Menu(file) SWTPUSH);

nextItemsetText(Next);

MenuItem seperator = new MenuItem(new Menu(file) SWTSEPARATOR);

MenuItem quitItem = new MenuItem(new Menu(file) SWTPUSH);

quitItemsetText(Quit);

//add listeners for the actions

prevItemaddListener(SWTSelection new Listener() {

public void handleEvent(Event e) {

Systemoutprintln(Previous menu item selected);

}

});

nextItemaddListener(SWTSelection new Listener() {

public void handleEvent(Event e) {

Systemoutprintln(Next menu item selected);

}

});

quitItemaddListener(SWTSelection new Listener() {

public void handleEvent(Event e) {

shelldispose();

}

});

}

private void clearText() {

fnameTextsetText();

lnameTextsetText();

phoneTextsetText();

emailTextsetText();

addrTextsetText();

miscTextsetText();

}

private void setTextEditable(boolean editable) {

fnameTextsetEditable(editable);

lnameTextsetEditable(editable);

phoneTextsetEditable(editable);

emailTextsetEditable(editable);

addrTextsetEditable(editable);

miscTextsetEditable(editable);

}

private void enableEditButtons(boolean enable) {

newButtonsetEnabled(enable);

deleteButtonsetEnabled(enable);

editButtonsetEnabled(enable);

}

private void enableSaveButtons(boolean enable) {

saveButtonsetEnabled(enable);

cancelButtonsetEnabled(enable);

}

}

()测试运行run就可以啦               

上一篇:安装Struts需要的软件

下一篇:Struts 2, spring 2, hibernate