步入SWT/JFace世界,你还等什么?

    本文主要讨论Java中的GUI图形库之一:SWT/JFace。在本文的第一部分,将解释什么是SWT/JFace以及如何安装SWT/JFace。在本文的第二部分将以实例的方式讨论如何使用SWT/JFace编写GUI程序。

   一、 进入SWT/JFace世界

  1. 什么是SWT/JFace

   Java是一种强大的编程语言。但强大就意味复杂,尤其是和Java相关的名词就象天上的星星一样,数都数不过来。在本文中就涉及到两个比较常用的名词 SWT和JFace。在标题中将SWT和JFace放到一起,并不是说SWT和JFace是一个意思,而是说它们的关系非常紧密。

  基 于Java的图形库最主要的有三种,它们分别是Swing、AWT和SWT。其中前两个是Sun随JDK一起发布的,而SWT则是由IBM领导的开源项目 (现在已经脱离IBM了)Eclipse的一个子项目。SWT的执行效率非常高。这是由于SWT的底层是由C编写的。由于SWT通过C直接调用系统层的 GUI API。因此,使用SWT编写GUI程序,在外观上就和使用C++、Delphi(在Windows下)编写的程序完全一样。它的这一点和AWT类似。 AWT在底层也是使用C直接调用系统层的GUI API。但它们是有区别的,最大的区别可能就是一个是Sun提供的,一个是Eclipse自带的。这就意味着如果使用AWT,只要机器上安装了JDK或 JRE,发布软件时无需带其它的库。而如何使用SWT,在发布时必须要自带上SWT的*.dll(Windows版)或*.so(Linux/Unix 版)文件以及相关的*.jar包。还有就是它们所提供的图形接口有一些差异。SWT可能更丰富一些,我们可以看看Eclipse的界面就知道了。但随着 Sun对AWT库的不断更新,AWT的图形表现能力也在不断地提高。

  虽然SWT很强大,但它比较底层。也就是说它的一些功能在使用上 还比较低级,不太符合面向对象的特征。因此,在SWT的基础上又开发了JFace。JFace在SWT上进行了一定的扩展。因此,也可说JFace是基于 SWT的,就象在VC中使用MFC来包装Win32 API一样。

  2. SWT/Face的安装

  在发布使用 SWT/JFace编写的GUI程序时,要随程序带上相应的库文件。对于Windows版的SWT来说,SWT包含有4个dll文件和一个jar文件。它 们是swt-awt-win32-3305.dll、swt-gdip-win32-3305.dll、swt-wgl-win32-3305.dll、 swt-win32-3305.dll和swt.jar。在发布时,要将4个dll文件放到path路径中,或者使用- Djava.library.path设置相应的路径。将swt.jar放到classpath路径中,或使用-classpath设置相应的jar包。 而对于JFace,除了上述的5个文件外,还要带上5个jar包:

org.eclipse.core.runtime_3.1.2.jar
org.eclipse.jface_3.1.1.jar
org.eclipse.jface.text_3.1.2.jar
org.eclipse.osgi_3.1.2.jar
org.eclipse.text_3.1.1.jar

  这5个jar包都可以在eclipse的plugins目录中找到,在这5个文件后面的版本号可能会因为eclipse的版本号不同而不同,但前面的部分都是一样的。读者在找这些jar包时应注意这一点。

  SWT的开发包可以从http://www.eclipse.org单独下载,也可以从eclipse的plugins目录复制。而JFace的开发包并未提供单独的下载,因此,JFace的开发包必须要从plugins目录得到。

二、 让我们编写第一个程序吧

   学习一种新技术的最好方法就是去使用它。下面就让我们来使用SWT和JFace来分别实 现同一个程序。这个程序是一个简单的记事本程序。在上面有三个按纽,分别是"新键"、"打开","保存",下面是一个文本框,用于编辑文本信息。下面让我 们先来看一下使用SWT实现的程序界面:


图1 使用SWT实现的记事本程序界面


  怎么样,看看上面的界面是不是和用Delphi、VC做的界面完全一样!!

  1. 用SWT实现

  不论一个程序带不带GUI,都必须有一个入口点,对于Java来说,这个入口点就是main函数。因此,在编写程序之前,我们必须定义一个类,并且这个类中必须有个main函数。

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import java.io.*;

public class FirstSWT
{
 // 用于记录是否已经打开或保存了一个文件,如果已经打开或保存了一个文件,
 // 这个变量就是这个文件的名子
 private static String fn = "";
 public static void main(String[] args)
 {
  … …
 }
}

  上面四个import将导入一些在本程序中要用到的jar包,前三个是SWT的包,最后一个是Java的标准输入输出包。

  1、建立窗体

  任何一个GUI程序,都至少有一个窗体(在本程序中只有一个窗体)。因此,下面我们就在main函数中建立这个窗体。

display = new Display();
shell = new Shell(display, SWT.DIALOG_TRIM);
shell.setText("第一个SWT程序");
shell.setSize(400, 300);

  在上面4行代码中涉及到了两个类:Display和Shell。这两个类都是在FirstSWT中定义的私有静态类,之所以定义成全局的,是因为在以后的按钮事件类中要使用它们。下面是它们的定义:

private static Display display;
private static Shell shell;

  后面2条语句通过调用Shell类的setText和setSize方法,设置了窗口的标题和尺寸。

  下面解释一下Display和Shell类是什么。

   SWT在底层实现上分为两层:系统层和用户层。系统层就是直接和操作系统平台打交道,系统层的存在依赖于操作系统平台。在这里,系统层就是 Display类。Display的功能就是在系统和用户之间架起一座桥梁,也就是说使用户访问系统资源透明化。而Shell类是直接和用户打交道,因 此,它属于用户层。通过Shell类可以控制窗体中的控件、窗体本身的属性等。而Shell通过Display这座桥梁访问系统级API。

  l 向窗体中添加控件

  接下来我们先在这个窗体上建立三个按钮,代码如下:

Button newButton = new Button(shell, SWT.PUSH);
newButton.setLocation(2, 5);
newButton.setSize(50, 20);
newButton.setText("新建");

Button openButton = new Button(shell, SWT.PUSH);
openButton.setLocation(60, 5);
openButton.setSize(50, 20);
openButton.setText("打开");

Button saveButton = new Button(shell, SWT.PUSH);
saveButton.setLocation(118, 5);
saveButton.setSize(50, 20);
saveButton.setText("保存");

  按钮类是Button,在建立时,Button需要两个参数,一个是Shell对象,另外一个是按钮的类型,在本例中,我们使用SWT.PUSH类型(一般的按钮类型)。

  注:和SWT相关的常量都定义在了SWT 中。

  后面3条语句分别设置了三个按钮的位置,尺寸和按钮标题。

  最后在3个按钮下方建立一个文本框

text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
text.setLocation(2, 30);
text.setSize(shell.getClientArea().width - 4, shell.getClientArea().height - 35);

  文本框的类是Text,和按钮不同的是,由于文本框需要在按钮事件中被访问,因此,文本对象必须定义成全局的。

private static Text text;

  1、添加控件事件代码

   现在让我们为三个按钮控件中加入事件代码。和大多数语言不同的是,按钮的单击事件不叫Click,而叫Selection。一般需要将 Selection事件代码放到一个从SelectionAdapter类继承的子类中。然后通过按钮类的addSelectionListener方法 将这个事件类的实例传入按钮类的实例中。但为了简便起见,我们使用隐式建立对象的方法来建立事件类的对象。下面是"新建"按钮的事件代码。

newButton.addSelectionListener(new SelectionAdapter()
{
 public void widgetSelected(SelectionEvent event)
 {
  fn = "";
  shell.setText("第一个SWT程序");
  text.setText("");
 }
});

  由于SelectionAdapter是一个抽象类,它有一个抽象方法widgetSelected,在上述代码被override了。在"新建"按钮中将全局文件名赋成空串,并将窗体的标题赋成初始状态,最后将文本框清空。

  接下来让我们看看"打开"按钮的事件代码:

openButton.addSelectionListener(new SelectionAdapter()
{
 public void widgetSelected(SelectionEvent event)
 {
  FileDialog dlg = new FileDialog(shell, SWT.OPEN);
  String fileName = dlg.open();
  try
  {
   if (fileName != null)
   {
    // 打开指定的文件
    FileInputStream fis = new FileInputStream(fileName);
    text.setText("");
    BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    String s = null;
    // 将指定的文件一行一行地加到文本框中
    while ((s = in.readLine()) != null)
     text.append(s + "/r/n");
   }
   if (fileName != null)
   {
    fn = fileName;
    shell.setText(fn);
    MessageBox successBox = new MessageBox(shell);
    successBox.setText("信息");
    successBox.setMessage("打开文件成功!");
    successBox.open();
   }
  }
  catch (Exception e)
  {
   MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
   errorBox.setText("错误");
   errorBox.setMessage("打开文件失败!");
   errorBox.open();
  }
 }
});

  上面代码的基本逻辑是使用打开对话框选择一个文件,使用FileInputStream将这个文件打开,并且将文件中的内容一行一行地加入到文本框中,如果这个过程失败,显示错识对话框,如果成功,将fn变量和窗体的标题栏都赋成这个文件名。

  最后让我们实现"保存"按钮事件的代码。

saveButton.addSelectionListener(new SelectionAdapter()
{
 public void widgetSelected(SelectionEvent event)
 {
  try
  {
   String fileName = null;
   if (fn.equals(""))
   {
    FileDialog dlg = new FileDialog(shell, SWT.SAVE);
    fileName = dlg.open();
    if(fileName != null)
     fn = fileName;
   }
   if (fn != "")
   {
    FileOutputStream fos = new FileOutputStream(fn);
    OutputStreamWriter out = new OutputStreamWriter(fos);
    out.write(text.getText());
    out.close();
    shell.setText(fn);
    MessageBox successBox = new MessageBox(shell);
    successBox.setText("信息");
    successBox.setMessage("保存文件成功!");
    successBox.open();
   }
  }
  catch (Exception e)
  {
   MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
   errorBox.setText("错误");
   errorBox.setMessage("保存文件失败!");
   errorBox.open();
  }
 }
});

  这段代码的基本逻辑是如果当前已经打开一个文件或已经将当前的新建文件保存过了,在点击"保存"按钮时,不再显示保存对话框,而直接将文件保存,否则,将显示一个保存对话框,通过这个对话框可以选择一个文件名,然后再保存。

 1、让我们最后画龙点睛吧

  程序到这已经基本完成了,但还需要进行最后一步,就是对事件进行监听。在main函数的最后,可以加上如下的代码实现这个功能。

shell.open(); // 显示窗体
while (!shell.isDisposed()) // 当窗体未被关闭时执行循环体内的代码
{
 // 如果未发生事件,通过sleep方法进行监视事件队列
 if (!display.readAndDispatch())
 {
  display.sleep();
 }
}
display.dispose(); // 释放底层的资源

  看看上面的代码,是不是有点象MFC的监听事件代码!!

  2、用JFace实现

  使用JFace实现GUI程序和SWT的最大的区别就是JFace的窗体类必须从ApplicationWindow继承。

import org.eclipse.jface.window.ApplicationWindow;

public class FirstJFace extends ApplicationWindow
{
 public static void main(String[] args)
 {
  … …
 }
}

  另外一个不同是在设置窗体上,JFace通过ApplicationWindow类提供一系列的事件函数,通过在这些函数中编写代码,可以很方便地对窗体进行操作。如可以在createContents函数中向窗体中加入控件。

protected Control createContents(Composite parent)
{
// 这里边的代码就是上述的建立控件的代码,只是要将shell换成parent
}

  由于使用JFace操作控件的方式和SWT类似,本文将不再详细讨论,感性趣的读者可以参考本文提供的源代码。使用JFace的程序界面和SWT完全一样,界面如图2所示:


图2使用JFace实现的记事本程序界面
 
示例1(SWT)的完整代码如下:

import  org.eclipse.swt.widgets. * ;
import  org.eclipse.swt. * ;
import  org.eclipse.swt.events. * ;
import  java.io. * ;

public   class  FirstSWT
{
    
private   static  Display display;

    
private   static  Shell shell;

    
private   static  Text text;

    
private   static  String fn  =   "" ;

    
public   static   void  main(String[] args)
    {
    display 
=   new  Display();
    shell 
=   new  Shell(display, SWT.DIALOG_TRIM);
    shell.setText(
" 第一个SWT程序 " );
    shell.setSize(
400 300 );

    
//  加入新建、打开和保存按钮
    Button newButton  =   new  Button(shell, SWT.PUSH);
    newButton.setLocation(
2 5 );
    newButton.setSize(
50 29 );
    newButton.setText(
" 新建 " );

    Button openButton 
=   new  Button(shell, SWT.PUSH);
    openButton.setLocation(
60 5 );
    openButton.setSize(
50 29 );
    openButton.setText(
" 打开 " );

    Button saveButton 
=   new  Button(shell, SWT.PUSH);
    saveButton.setLocation(
118 5 );
    saveButton.setSize(
50 29 );
    saveButton.setText(
" 保存 " );

    
//  加入一个文本框
    text  =   new  Text(shell, SWT.MULTI  |  SWT.BORDER  |  SWT.V_SCROLL  |  SWT.WRAP);
    text.setLocation(
2 40 );
    text.setSize(
391 ,
        
227 );

    
//  加入新建按纽事件
    newButton.addSelectionListener( new  SelectionAdapter()
    {
        
public   void  widgetSelected(SelectionEvent event)
        {
        fn 
=   "" ;
        shell.setText(
" 第一个SWT程序 " );
        text.setText(
"" );
        }
    });

    
//  加入打开按钮事件
    openButton.addSelectionListener( new  SelectionAdapter()
    {
        
public   void  widgetSelected(SelectionEvent event)
        {

        FileDialog dlg 
=   new  FileDialog(shell, SWT.OPEN);
        String fileName 
=  dlg.open();

        
try
        {
            
if  (fileName  !=   null )
            {
            FileInputStream fis 
=   new  FileInputStream(fileName);

            text.setText(
"" );

            BufferedReader in 
=   new  BufferedReader(
                
new  InputStreamReader(fis));
            String s 
=   null ;
            
while  ((s  =  in.readLine())  !=   null )
                text.append(s 
+   " " );

            }
            
if  (fileName  !=   null )
            {
            fn 
=  fileName;
            shell.setText(fn);
            MessageBox successBox 
=   new  MessageBox(shell);
            successBox.setText(
" 信息 " );
            successBox.setMessage(
" 打开文件成功! " );
            successBox.open();
            }
        }
        
catch  (Exception e)
        {
            MessageBox errorBox 
=   new  MessageBox(shell, SWT.ICON_ERROR);
            errorBox.setText(
" 错误 " );
            errorBox.setMessage(
" 打开文件失败! " );
            errorBox.open();

        }

        }
    });

    
//  添加保存按钮事件
    saveButton.addSelectionListener( new  SelectionAdapter()
    {
        
public   void  widgetSelected(SelectionEvent event)
        {

        
try
        {
            String fileName 
=   null ;
            
if  (fn.equals( "" ))
            {
            FileDialog dlg 
=   new  FileDialog(shell, SWT.SAVE);
            fileName 
=  dlg.open();
            
if  (fileName  !=   null )
                fn 
=  fileName;
            }
            
if  (fn  !=   "" )
            {
            FileOutputStream fos 
=   new  FileOutputStream(fn);
            OutputStreamWriter out 
=   new  OutputStreamWriter(fos);
            out.write(text.getText());
            out.close();
            shell.setText(fn);
            MessageBox successBox 
=   new  MessageBox(shell);
            successBox.setText(
" 信息 " );
            successBox.setMessage(
" 保存文件成功! " );
            successBox.open();
            }

        }
        
catch  (Exception e)
        {
            MessageBox errorBox 
=   new  MessageBox(shell, SWT.ICON_ERROR);
            errorBox.setText(
" 错误 " );
            errorBox.setMessage(
" 保存文件失败! " );
            errorBox.open();

        }
        }
    });

    shell.open();
    
while  ( ! shell.isDisposed())
    {
        
if  ( ! display.readAndDispatch())
        {
        display.sleep();
        }
    }
    display.dispose();
    }

}


示例2(JFace)的完整代码如下:
import  java.io. * ;
import  org.eclipse.jface.window.ApplicationWindow;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.events.SelectionAdapter;
import  org.eclipse.swt.events.SelectionEvent;
import  org.eclipse.swt.widgets. * ;

public   class  FirstJFace  extends  ApplicationWindow
{
    
private  Text text;

    
private  String fn  =   "" ;

    
private   static  Shell myShell;

    
public  FirstJFace()
    {
    
super ( null );
    }

    
public   void  show()
    {
    setBlockOnOpen(
true );
    setShellStyle(SWT.DIALOG_TRIM);
    open();
    Display.getCurrent().dispose();
    }

    
protected   void  configureShell(Shell shell)
    {
    
super .configureShell(shell);
    shell.setLayout(
null );
    shell.setText(
" 第一个JFace程序 " );
    shell.setSize(
400 300 );
    myShell 
=  shell;
    }

    
protected  Control createContents(Composite parent)
    {
    
//  加入新建、打开和保存按钮
    Button newButton  =   new  Button(parent, SWT.PUSH);
    newButton.setLocation(
2 5 );
    newButton.setSize(
50 25 );
    newButton.setText(
" 新建 " );

    Button openButton 
=   new  Button(parent, SWT.PUSH);
    openButton.setLocation(
60 5 );
    openButton.setSize(
50 25 );
    openButton.setText(
" 打开 " );

    Button saveButton 
=   new  Button(parent, SWT.PUSH);
    saveButton.setLocation(
118 5 );
    saveButton.setSize(
50 25 );
    saveButton.setText(
" 保存 " );

    
//  加入一个文本框
    text  =   new  Text(parent, SWT.MULTI  |  SWT.BORDER  |  SWT.V_SCROLL
        
|  SWT.WRAP);
    text.setLocation(
2 30 );
    text.setSize(parent.getClientArea().width 
-   4 ,
        parent.getClientArea().height 
-   35 );

    
//  加入新建按纽事件

    newButton.addSelectionListener(
new  SelectionAdapter()
    {
        
public   void  widgetSelected(SelectionEvent event)
        {
        fn 
=   "" ;
        myShell.setText(
" 第一个JFace程序 " );
        text.setText(
"" );
        }
    });

    
//  加入打开按钮事件
    openButton.addSelectionListener( new  SelectionAdapter()
    {
        
public   void  widgetSelected(SelectionEvent event)
        {

        FileDialog dlg 
=   new  FileDialog(myShell, SWT.OPEN);
        String fileName 
=  dlg.open();

        
try
        {
            
if  (fileName  !=   null )
            {
            FileInputStream fis 
=   new  FileInputStream(fileName);

            text.setText(
"" );

            BufferedReader in 
=   new  BufferedReader(
                
new  InputStreamReader(fis));
            String s 
=   null ;
            
while  ((s  =  in.readLine())  !=   null )
                text.append(s 
+   " " );

            }
            
if  (fileName  !=   null )
            {
            fn 
=  fileName;
            myShell.setText(fn);
            MessageBox successBox 
=   new  MessageBox(myShell);
            successBox.setText(
" 信息 " );
            successBox.setMessage(
" 打开文件成功! " );
            successBox.open();
            }
        }
        
catch  (Exception e)
        {
            MessageBox errorBox 
=   new  MessageBox(myShell,
                SWT.ICON_ERROR);
            errorBox.setText(
" 错误 " );
            errorBox.setMessage(
" 打开文件失败! " );
            errorBox.open();

        }

        }
    });

    
//  添加保存按钮事件
    saveButton.addSelectionListener( new  SelectionAdapter()
    {
        
public   void  widgetSelected(SelectionEvent event)
        {

        
try
        {
            String fileName 
=   null ;
            
if  (fn.equals( "" ))
            {
            FileDialog dlg 
=   new  FileDialog(myShell, SWT.SAVE);
            fileName 
=  dlg.open();
            
if  (fileName  !=   null )
                fn 
=  fileName;
            }
            
if  (fn  !=   "" )
            {
            FileOutputStream fos 
=   new  FileOutputStream(fn);
            OutputStreamWriter out 
=   new  OutputStreamWriter(fos);
            out.write(text.getText());
            out.close();
            myShell.setText(fn);
            MessageBox successBox 
=   new  MessageBox(myShell);
            successBox.setText(
" 信息 " );
            successBox.setMessage(
" 保存文件成功! " );
            successBox.open();
            }

        }
        
catch  (Exception e)
        {
            MessageBox errorBox 
=   new  MessageBox(myShell,
                SWT.ICON_ERROR);
            errorBox.setText(
" 错误 " );
            errorBox.setMessage(
" 保存文件失败! " );
            errorBox.open();

        }
        }
    });

    
return   null ;
    }

    
public   static   void  main(String[] args)
    {
    
new  FirstJFace().show();
    }
    
private   void  createActions()
    {
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值