专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 行业应用

SWT基础概念小结

发布时间:2010-06-06 18:56:52 文章来源:www.iduyao.cn 采编人员:星星草
SWT基础概念总结

    实例变量也被翻译成"""成员变量"。在面向数据库的实体类中叶被称为

"属性""字段"的变量。Hibernate中也称为POJO,即简单原始的Java变量。使

用变量的一般原则是:尽量使变量的有效范围最小化,即优先考虑用局部变量。其次是

实例变量,最后才是类变量。

    还有一种常量的写法,比类常量前多了一个final,如下:

    final static int ALL_CLICKS =  0;  //something

    ALL_CLICKS全是大写的,这是常量的规范式命名方式。这时的ALL_CLICKS

final约束,它不能再被赋值了。

 

SWT Button分为四类样式。他们分别是

SWT.PUSH:这是最正常的按钮

SWT.CHECK

SWT.RADIO:和上面的有点相似。

SWT.ARROW

Button中间的字体可以调节向左或着居中什么的。他的表面还提供了深凹陷和平面型等外观设置。

使用符号"|"可以让一个控件(如:Button)应用多个样式。

如:new Button(shell,SWT.LEFT|SWT.BORDER|SWT.CHECK);

其方法参数:new Button(Composite parent,int style)

 

    标签类:Label   LabelSWT中最简单的界面组件,给出一个类实例如下:

Label label = new Label(shell,SWT.NONE);

Label.setBounds(38,21,100,18);

Label.setText("姓名")

    Label类提供了样式列表。包括居中、靠左等,SWT.WRAP:自动换行。还有SWT.BORDER:深陷型。他还有分栏符的功能。 。分栏符有横有竖。SWT.SEPARATOR等。

 

    文本框类:Text   文本框类也是比较简单的界面组件,但他很重要。

他可以限定你最多输入多少个字符:setTextLimit(int n).

 

    MessageDialog MessageDialog调用shell null做参数的时候,是有区别的。当MessageDialog调用shell作为参数而弹出提示窗口的时候,Windows的任务栏不会新增一个任务项;当MessageDialog调用null的时候,Windows任务栏会多出来一个任务项。

 

    下拉框类:Combo

    下拉框样式:

    下拉框设置:

1)  combo中显示字符,for(int I =1;I<=10;I++);combo.add(int i)

2)  combo中显示字符 Combo.setItems(new String[]{"第一项", "第二项""第三项"});

3)  设置第一项为当前项Combo.select0

4)  Combo的各项与 陌生类型对象 实现对应关系:setData(key,Object);他将对象(Objcet)对应一个键值(key),然后附在combo上。键值可以使用任意字符,key对应的是comboSelectionIndex的值(初始值为0)。看下例:

for(int i=1;i<11;i++)

{

     Combo.add(""+ i + "");

     Combo.setData(i-1,new Integer(i));

}   关于Combo还有更多细致代码操作,可参考Eclipse从入门到精通 6869页内容。

 

列表框类:List

    ListCombo有很多相似点,但有一点不同:List可以选择多项,而Combo只能选择一项。故:List没有getText()方法。List取值用的是getSelection()方法,返回的是所有选项组成的数组。

 

布局管理器:Layout

 

FillLayout:是最简单的布局管理器,他把组件摆成一行或一列,并强制组件的大小与其一致。他里面的组件都会尽量充满这个容器。FillLayout不能折行,不能设置边界距离和间距。    FillLayout一般使用于任务栏、工具栏、Group中的一组复选框或者容器内只有一个组件的时候。例如,如果一个Shell内只有一个Group组件,那么将FillLayout用于Shell,则其内的Group将完全充满Shell。他在SWT界面设中很常用。

2个示例代码段:

Shell.etLayout(new FillLayout());//FillLayout对象应用于shell上。

new Button(shell,SWT.NONE).setText("sure");//Button建于shell中。

 

 

 

 

 

 

 

shell中有两个组件的情况会是这样的:

Shell.etLayout(new FillLayout());//FillLayout对象应用于shell上。

new Button(shell,SWT.NONE).setText("sure");//Button建于shell

new Button(shell,SWT.NONE).setText("cancel");

 

这两个按钮的排列方式是可以更改的,比如让这两个按钮横着放,你要在new FillLayout()这里动手。改成:new FillLayout(SWT.VERTICAL);

 

 

RowLayout:行列式布局管理器。可以使组件折行显示,并可以设置边界距离和间距。他还可以对每个组件通过setLayoutData方法设置RowData对象,RowData用来设置组件的大小。

RowLayout rowLayout = new RowLayout();

 

rowLayout.marginTop = 30;//组件距离容器上边缘30个像素间隔

rowLayout.marginLeft = 59//左边 50个。

rowLayout.spacing = 3;//组件间的间隔为3个像素

 

shell.setLayout(rowLayout);//shell应用RowLayout布局

//shell里建立3个按钮

New Button(shell,SWT.NONE).setText("b1");

New Button(shell,SWT.NONE).setText("button2");

New Button(shell,SWT.NONE).setText("button3");

 

窗口宽度不足,按钮自动折行rowLayout.wrap = true;

 

可以使用rowLayout.pack控制组件大小,默认值为true

rowLayout.pack = false的作用是让组件大小相同。

 

 

使用rowLayout.justify控制空间伸展,默认值为false

rowLayout.justify = true的作用是让组件可根据空间进行伸展。如下图:

 

 

使用RowData更改按钮外观,对组件的大小、形状、表现形式会有影响。RowData被称为布局数据类,他是RowLayout专用类,对其他Layout不起作用。GridLayout有自己的专用布局数据类:GridData。他们的作用一样:改变容器中组件的外观形状。

RowData最常用的定义格式:new RowData(int width,int height);

代码示例:

Shell.setLayout(new RowLayout());

Button Bi = new Button(shell,SWT.NONE);

Bi.setText("BUTTON 1");

Bi.setLayoutData(new RowData(30,50));

New Button(shell,SWT.NONE).setText("BUTTON 2");效果图如下:

 

 

GridLayout:网格布局管理器。GridData的使用很复杂。

示例代码:

GridLayout gl = new GridLayout();//生成一个GridLayout对象

shell.setLayout(gl);//gl布局应用于shell面板

//Creat Five Buttons

new Button(shell,SWT.NONE).setText("b1");

new Button(shell,SWT.NONE).setText("BUTTON 2");

new Button(shell,SWT.NONE).setText("b3");

new Button(shell,SWT.NONE).setText("BUTTON 4");

new Button(shell,SWT.NONE).setText("BUTTON 5");

 

设置列数:gl.numColumns,他的默认值为1. numColumnsGridLayout的重要属性,通常他是第一个设置的属性。如:gl. numColumns = 2,是指把容器分为两列,组件从左道又摆放在那里,这时第三个组件将自动添加新的一行。

Gridlayout  gl  =   new GridLayout();

gl. numColumns = 3 //

shell.setLayout(gl);

另一种写法是:用GridLayout的另一个构造函数:

shell.setLayout(new GridLayout(3,false));不同numColumns值的效果图如下:

 

 

使用makeColumnsEqualWidth把组件等距分开,默认值为false

效果如下:

 

组件都是向左紧挨着排列的,但如果加一句:gridLayout.makeColumnsEqualWidth = true;

则组件会按等距分开。

 

前面所讲的内容只能控制容器的布局,例如将容器分成几列,没列间隔多少,列是否平分宽度等。但容器中的组件的外观形状并没有改变!比如:让一个按钮加宽到占用两个列,或者加长到占用两行,让按钮跳过一列排到下一行等。但是:如果你用按钮本身的设置长宽的方法来实现这些效果,虽然他可以使按钮变宽、变长,然而他也影响到了其他组件的布局,并没有打到我们真正想要的效果。

     GridDataGridLayout相结合可以变换出很多复杂的布局方式。一个GridData对象只能用于一个组件,GridData是不能多个组件共用的,否则界面会紊乱。

      GridData用法演示:

1.    horizotalSpan。设置组件占用的列数。

Shell.setLayout(new GridLayout(2,false));//两列

New Button(shell,SWT.NONE).setText("b1");

New Button(shell,SWT.NONE).setText("Button2");

 

Button button  =  new  Button(shell,SWT.NONE);

GridData  gridData = new GridData();//define a GridData Object.

gridData.horizotalSpan = 2//Button抢占两个列的空间。

 

Button.setLayoutData(gridData);

Button.setText("b3");

New Button(shell,SWT.NONE).setText("Button4");

New Button(shell,SWT.NONE).setText("Button5");

 

2.    GridData.HORIZONTAL_ALLGN_FILL对其式充满。

Shell.setLayout(new GridLayout(2,false));//两列

New Button(shell,SWT.NONE).setText("b1");

New Button(shell,SWT.NONE).setText("Button2");

 

Button button  =  new  Button(shell,SWT.NONE);

GridData  gridData = new GridData(HORIZONTAL_ALLGN_FILL);

//define a GridData Object.

gridData.horizotalSpan = 2//Button抢占两个列的空间。

 

Button.setLayoutData(gridData);

Button.setText("b3");

New Button(shell,SWT.NONE).setText("Button4");

New Button(shell,SWT.NONE).setText("Button5");

 

3.    水平抢占式充满:GridData.FILL_HORIZONTAL

 

 

单独选定button 5水平抢占:

将这句代码New Button(shell,SWT.NONE).setText("Button5");

改为:

Button button5  =  new  Button(shell,SWT.NONE);

GridData  gridData2  =  new  GridData(GridData.HORIZONTAL_ALIGN_FULL);

button5.setLayoutData(gridData2);

button5.set("button5");

 

 

 

4.    GridData.FILL_BOTH双向抢占式

他相当于:FILL_HORIZONTAL(水平抢占式)和FILL_VERTICAL(垂直抢占式)的合并效果。

 

 

5.    gridData.horizontalAlignment and gridData.verticalAlignment.

这两个属性用来设置组件在格子里的对其方式,使用方法如下:

gridData.horizontalAlignment = GridData.CENTER;以下是4种不同效果

 

 

 

 

 

 

6.    gridData.horizontalIndent属性可以使组件向右移动指定长度。

此属性只有当horizontalAlignment设为BEGINNING(默认值)时有效。以下代码将b5向右移动10个像素:

GridData gridData  =  new  GridData();

gridData.horizontalIndent  =  10 ;l

b5.setLayoutData(gridData);

 

 

7.    动态缩扩:gridData.grabExcessHorizontalSpace||gridData.grabExcessBerticalSpace (默认值:false)

这两个属性主要用在Textlist中。效果是:当组件所在容器大小改变时,组件的占有空间也自动增大或减小。例如:设置一个Text组件为:grabExcessHorizontalSpace  =  true,那么当用户调整窗口,导致容器的宽度变化时,Text所在列空间也会随着变化,但同一行上的其他组件会保持宽度不变。

 

强调:改变的是他们占有的空间,但他们本身的大小不会改变。要让他们本身的大小也随着改变,还要加一句:gridData.horizontalAligment = GridData.FULL

代码如下:

//3列,且makeColumnsEqualWidth = false

shell.setLayout(new GridLayout(3,false));

new Button(shell,SWT.NONE).setText("button1");

new Button(shell,SWT.NONE).setText("button2");

new Button(shell,SWT.NONE).setText("button3");

new Button(shell,SWT.NONE).setText("button4");

Button button  =  new  Button(shell,SWT.NONE);

GridData  gridData  =   new GridData();//define a  gridData object.

gridData.gradExcessHorizontalSpace  =  true ;//空间横向随容器变化

button.setLayout(gridData);

button.setText("b5");

8.    改变组件的宽度和高度:gridData.widthHintheightHint

下列代码改变b5为:200    15

 

1 Button  button  =  new  Button(shell,SWT.NONE);

2 GridData  gridData   =  new  GridData();

3 gridData.widthHint  = 100;

4 gridData.heightHint  =  15;

5 button.setLayoutData(gridData);

6 button.setText("b5");

由上图可看,b5大小不随窗口变化。但是,在下面这句代码被改写了时,会出现矛盾。当与GridLayout其他设置相矛盾其表现如下:

2 GridData  gridData  = new GridData(GridData.FILL_HORIZONTAL);

 

 

 

    堆栈式LayoutStackLayout

    他像一副叠在一起的扑克牌,一叠UI面板只显示位于最上面的那个面板。

 

    右边部分就是一个用了堆栈式布局管理的容器,当左边选择不同的项时,右边部分就显示这一项相应的设置组件。Eclipse从入门到精通9293

 

    表格式布局管理器FormLayout

    FormLayoutGridLayout一样强大,而且还很灵活,精确。这个布局是SWT2.0新增的。他们可以做成同样的效果,但有时使用后者更有效,它不会像GridLayout因为容器大小变化而导致布局错位。

1.   设置边距:marginWidth marginHeight

他们用来设置左边距和右边距。

FormLayout  formLayout  =   new  FormLayout();

formLayout.marginWidth  =  100;

formLayout.marginHeight  =  50;

shell.setLayout(formLayout);

new Button(shell,SWT.NONE).setText("button 1");

 

2.   FormData构造函数

FormLayout的布局数据类,使用方法:

new  FormData() new FormData(int width, int height);

示例代码:

shell.setLayout(new FormLayout());

//new FormData();

Button  button1  =  new  Button(shell,SWT.NONE);

Button1.setText("button1");

FormData  formData  =  new  FormData();

button1.setLayoutData(formData);

 

Button  button2  =  new  Button(shell,SWT.NONE);

button2.setText("button2");

FormData  formData2  =  new  FormData(200,50);//button2200,宽50

button2.setLayoutData(formData2);

 

 

3.   FormAttachment类用法

FormAttachmentFormData下的更进一步的布局数据类,他的用法主要体现在它不同的构造函数中。

new FormAttachment(int numerator , int offset)

numerator:分子,占得百分比

offset:偏移量,在原来位置下移多少个像素。

button1的顶边(formData.top)shell容器的空白边距是shell容器总体空白长度的60%,偏移点数(points)0

 

 

 

shell.setLayout(new FormLayout());

new  Text(shell,SWT.BORDER).setText("text 1");

//button1应用于FormData

Button  button1  =  new  Button(shell,SWT.NONE);

button1.setText("button1");

FormData  formData  =  new FormData();

//button1顶端应用FormAttachment设置

formData.top =  new  FormAttachment(60,0);

button1.setLayoutDataformData);

**********************************************************

如果formData.top =  new  FormAttachment(60,0);变成

formData.top =  new  FormAttachment(60,10);则按钮向下移动10单位

 

**********************************************************

**********************************************************

new FormAttachment(Control control,int offset, int alignment)

参数1是一个Control类,一般在上用的时候都传入一个组件,如文本框等,来作参数。应用此FormAttachment的 组件将依据参数1control为标准布局,offsetcontrol的偏移量(单位:像素)alignment为对其方式。

shell.setLayout(new FormLayout());

Text  text1  = new  Text(shell,SWT.BORDER);

text1.setLayoutData(new  FormData(100,50));

FormData  formData   =  new  FormData();

//text1为基准偏移50

FormAttachment  formA  = new  FormAttachment(text1,50);

formData.top = formA;

formData.left = formA;

Button  button1  =  new  Button(shell,SWT.NONE);

button1.setText("button 1");

button1.setLayoutData(formData);//效果图如下:

 

FormAttachment(text1,50, int alignment)alignment为对其方式。

FormAttachment(text1,50)相当于默认alignment = SWT.DEFAULT

 

 

 

 

 

其他SWT组件

工具栏类ToolBar类、ToolItem类、ViewForm

ViewForm是一个容器,他是编辑器的基座,用来容纳工具栏和文本框。

ToolBar是一个工具栏,同时也是一个容器,他容纳工具栏的一个个按钮:ToolItem

 

ToolItem样式如下:

SWT.PUSH:普通按钮。

SWT.CHECK:单击按钮则下陷,再单击则会弹起来。

SWT.RADIO:一组RADIO类型按钮,如果一个按下,其他都会弹起来。

SWT.SEPARATOR按钮间的分割线

SWT.DROP_DOWN:旁边带个下拉条的按钮。

 

动态工具栏类CoolBar类、CoolItem

CoolBar组件是一个可以放置多个工具栏的容器类,基于CoolBar做出的工具栏可以通过拖动操作来改变排放位置,这种工具栏也被称为CoolBar类型工具栏。

 

CoolBar是一个容器类,它需要结合其子项CoolItem来使用。要得到Coolbar型工具栏,必须是CoolBarCoolItemToolBarToolItem四者一起使用。他们的关系图普如下所示:

ToolBar是建立在CoolBar容器里的,且CoolItem并非用来显示按钮,而是用来帮助CoolBar控制ToolBar的。比较可以发现CoolBarCoolItemToolBar三者的关系正好与TabFolderTabItemGroup三者的关系是一样的。

 

 

 

菜单类Menu类、MenuItem类。

 

示例代码如下:

Menu  mainMenu  =  new  Menu(shell,SWT.BAR);

shell.setMenuBar(mainMenu);

{

    //"文件"

    MenuItem  fileItem = new MenuItem(mainMenu,SWT.CASCADE);

    fileItem.setText("文件(&F)");

 

    //文件菜单

    Menu fileMenu = new Menu(shell,SWT.DROP_DOWN);

    fileItem.setMenu(fileMenu);

    {

        //新建 项

        MenuItem newFileItem  = new MenuItem(fileMenu,SWT.CASCADE);

        newFileItem.setText("新建&(F)");

//新建 菜单

        Menu newFileMenu  =  new  Menu(shell,SWT.DROP_DOWN);

        newFileItem.setMenu(newFileMenu);

        {

            //新建项目 

            MenuItem  newProItem = new MenuItem(newFileMenu,SWT.PUSH);

            //"\t"相当于间隔Tab键空间

            newProItem.setText("项目\tCtrl+Shift+N");

            //定义快捷键Ctrl+Shift+N

            newProItem.setAccelerator(SWT.CTRL+SWT.SHIFT+N);

            //设置菜单项的图标

            newProItem.setImage(new Image(null,"icons/project.gif"));

            //菜单项的单击事件

            newProItem.addSelectionListener(new SelectionAdapter(){

                public void widgetSelected(SelectionEvent e{

                    MessageDialog.openInformation(null,"","新建项目");}

            });

            //建立其他菜单项

            new MenuItem(newFileMenu,SWT.SEPARATOR);

            new MenuItem(newFileMenu,SWT.PUSH).setText("");

            new MenuItem(newFileMenu,SWT.PUSH).setText("");

            new MenuItem(newFileMenu,SWT.PUSH).setText("接口");

            new MenuItem(newFileMenu,SWT.SEPARATOR);//分隔符

            new MenuItem(newFileMenu,SWT.PUSH).setText("其他(&O)");

        }

    }

    new MenuItem(fileMenu,SWT.CASCADE).setText("退出");

}

//"文件"

    MenuItem  menuItem = new MenuItem(mainMenu,SWT.CASCADE);

    menuItem.setText("帮助(&H)");

 

Menu样式罗列如下:

SWT.BAR:用于主菜单

SWT.DROP_DOWN:用于子菜单

SWT.POP_UP:用于右键菜单

 

MenuItem样式罗列如下:

SWT.CHECK:选择后前面会出现一个小勾。

SWT.CASCADE:有子菜单的。

SWT.PUSH:普通型。

SWT.RADIO:选择后前面会显示一个圆点。

SWT.SEPARATOR:分隔符。

 

右键弹出式菜单,效果如下:

 

上述程序开头更改如下:

//Menu  mainMenu  =  new  Menu(shell,SWT.BAR);

//shell.setMenuBar(mainMenu);

Menu  mainMenu  =  new  Menu(shell,SWT.POP_UP);

shell.setMenuBar(mainMenu);

 

滑动条(Slider)、刻度条(Scale)和进度条(ProgressBar)

1.  滑动条:Slider

滑动条窗口里有连个组件:右边是一个滑动条,左边是一个用来显示滑动条当前值的文本框,其样式如下图所示:

 

代码示例:

shell.setLayout(new RowLayout());

final Text  text  =  new  Text(shell,SWT.BORDER);

//创建一个滑动条,默认水平,用SWT.VERTICAL可变垂直

final Slider  slider  =  new Slider(shell,SWT.NONE);

silder.setMinimum(0);

silder.setMaximun(100);

slider.setPageincrement(10)//翻页间隔值

slider.setSelection(25);//初始值

//监听滑动条值的改变,并将值显示在文本框中。

slider.addSelectionListener(new SelectionAdapter(){

     public void widgetSelected(SelectionEvent e){

         text.setText("" +  slider.getSelection());

     }

});

 

2.  刻度条:Scale

 

final Scale scale = new Scale(shell,SWT.NONE);

 

3.  进度条:ProgressBar

 

主要也是三种方法:setMinimum setMaximum setSelection

setSelection方法控制进度条的当前位置,代码如下:

 

shell.setLayout(new RowLayout());

//平滑型样式:SWT.SMOOTH,默认方格型。

final ProgressBar progBar  =  new  ProgressBar(shell,SWT.SMOOTH);

progBar.setMinimum(0);

progBar.setMaximum(100);

//创建一个GO按钮

Button  bg  =  new Button(shell,SWT.BORDER);

bg.setText("GO");

bg.addSelectionListener(new SelectionAdapter(){

     public void widgetSelected(SelectionEvent e){

         //每次循环前进一格

         for(int i=0;i<10;i++){

             try{

                 Thread.sleep(1000);

             }catch(Throwable e2){}//间隔一秒

             progBar.setSelection(i*10);

         }

     }

});

JFace包中有一种Dialog组件叫做:ProgressMonitorDialog,他也有表示任务执行进度的功能。

 

 

画布:Canvas

    Canvas类主要用于显示图像,即可以在Canvas上画图,也可以直接将图片显示其中。设置图像、更换图像、清除图像。P113有示例代码。

 

 

 

表格类:Table

前面介绍了选项卡的TabFolderTabItem,菜单的MenuMenuItem,工具栏的ToolBarToolItem,同样,Table也有自己的ItemTableItemTableItem代表表格中的一条记录。不过在实际项目中,特别是数据库项目一般都不用Table,而用JFace中基于Table扩展的TableViewer来作为表格组件。

 

 

 

 

 

示例代码:

shell.setLayout(new FillLayout);

/*

 * 创建一个Table对象,在央视里设置它为:可多选,全列选择

 * 并设置它的表头和表格线

*/

final Table table = new Table(shell,SWT.MULTI|SWT.FULL_SELEOTION);

table.setHeaderVisible(true);

table.setLinesVisible(true);

//Table增加两列,并设置列宽都是80

TableColumn  col1  =  new  TableColumn(table,SWT.NONE);

col1.setText("ID");

col1.setWidth(80);

TableColumn  col2  =  new  TableColumn(table,SWT.NONE);

col2.setText("姓名")

col2.setWidth(80);

//创建一个Composite容器,放入两个按钮

Composite  comp  =  new  Composite(shell,SWT.NONE);

comp.setLayout(new  RowLayout());

Button  addButton  =  new Button(comp,SWT.NONE);

addButton.setText("新增");

addButton.addSelectionListener(new SelectionAdapter(){

    int i = 1;

    public void widgetSelected(SelectionEvent e){

        TableItem  tableItem  =  new  TableItem(table,0);//

        tableItem.setText(new String[]{"" + "Sister Qin"}

 

        i++;

    }

});

Button  removeButton  =  new Button(comp,SWT.NONE);

removeButton.setText("删除");

removeButton.addSelectionListener(new  SelectionAdapter(){

    public void  widgetSelected(SelectionEvent e){

        //得到装有所有被选择记录的序号的数组

        int[] setlnice  =  table.getSelectionIndices()

        //Table根据数组中的序号将Item删除

        table.remove(sellnices);

    }

});

//监听Table的鼠标双击事件

table.addMouseLintener(new MouseListener(){

    public void mouseDoubleClick(MouseEvent e){

        int selIndex =table.getSelectionIndex();

        TableItem  item  =  table.getItem(SelIndex);

        String str  = "1=" + item.getText(0) + "\n 2=" + item.getText(1);

        MessageDialog.openInformation(null,null,str);

    }

    public void mouseDown(MouseEvent e){}

    public void mouseUp(MouseEvent e){}

});

 

 

树:Tree

    Table一样,Tree也有自己的ItemTreeItem,由它表示树中的每一个节点。JFace也有一个基于Tree扩展的TreeViewer,实际用到数据库的项目一般都用它。

117

 

表格型树:TableTree

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

    SWT中图像时Image类,它属于:org.eclipse.swt.graphics包。他有多种构造函数,最常用的是:new Image(Device device , String filename)。其中Device称为:设备

它包含Display(显示)Printer(打印)两种设备。如果要在屏幕上显示,Display适用。

 

    以下代码是使用 "绝对路径"来得到一个图像,并设置图像背景为红色,然后显示在一个按钮上。

    button.setText("button");

    Image image  =  new Image(display," C:\\refresh.gif ");

    image.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED);

    button.setImage(image);

 

    new Image(null , "c:\\refresh.gif");他的第一个参数为null,这时会默认使用当前的Display来做显示设备。

    如果在指示的路径没发现图片,会弹出错误提示。因为你不知道用户会将软件安装在什么目录下,故采用绝对路径的做法往往会出现问题,最好采用相对路径:

button.setImage(new Image(display , "icons/refresh.gif"));相对路径的起点是项目主目录,如:C:\\eclipse\workspac\myswt\icons\refresh.gif

    如果开发插件,由于插件是基于Eclipse运行环境的,所以还可以利用Eclipse自带的图像。方法如下:

ISharedImage  sharedImage =  PlathformUI.getWorkbench().getSharedImages();

Image  image  =  sharedImage.getImage(ISharedImages.IMG_OBJS_WARN_TSK);

 

图像类存在的问题:它是比较重量级的对象,内存占用很大,一般来说不再使用的Image对象应该用Imagedispose()方法立即释放掉。像Image这样的情况还有Font(字体)Color(颜色)等。

 

图像描述符:ImageDescriptor

由于Image存在的一些问题,JFace提供了一个轻量级的ImageDescriptor类,它属于:org.eclipse.jface.resource包。ImageDescriptor不存储图像本身,只有程序需要时候才创建。ImageDescriptor是一个抽象类,他不能用new来创建,它有两个静态方法:createFormFilecreateFormURL来生成图像描述符的方法。

 

 

1.  createFormFile方法

ImageDescriptor imageDesc = ImageDescriptor.createFormFile(

abc.class,"icons\*.gif);

 

代码含义:当要创建图像时,会通过Abc.class类所在目录下的icons子目录来加载*.gif。此时,imageDesc指示包含了指向*.gif的信息,还没有真正加载*.gifabc.class 可以是项目中的其他类,甚至是写这个语句的类也可以。

2.  createFormURL 方法

创建一个图像描述符,图像定位在C:\\icons\refresh.gif路径

URL  url  =  new URL(file:\\C:\\icons\\refresh.gif);

ImageDescriptor  imageDesc = ImageDescriptor.createFormURL(url);

 

 

创建一个图像描述符

        URL  url  =  new  URL("file:icons/refresh.gif");

ImageDescriptor  imageDesc = ImageDescriptor.createFormURL(url);

注意!这里会抛出异常,注意捕捉

 

Eclipse自带图像

        用于开发基于Eclipse运行环境的插件

ImageDescriptor  imageDesc =

WorkbenchImages.getImageDescriptor(WorkbenchGraphicConstants.IMG_ETOOL_HOME_NAV);

 

ImagaeDescriptorImage好处是:如果所指路径错误,则会自动用一个红方块图像来代替,而不会像Image那样弹出一个错误提示框。另外,由ImageDescriptor也可以得到Image,语句如下:

Image image  =   imageDesc.createImage();

 

图像注册表:ImageRegistry

JFace提供了一个能告诉缓存图像和图像描述的图像注册表。他的设计思路是:先创建指向图像的图像描述符,然后将图像描述符加上一个键值添加到图像注册表中去,这个键值是和图像描述符一一对应的。当希望获得图像时,就可以用键值从注册表中取得。

 

 

 

附件中的文件是原文,带有图片

 

 

 

 

 

 

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

  • 《松本行弘的程序全世界》之面向对象

    《松本行弘的程序世界》之面向对象 最近读《SICP》把脑细胞搞死大半,还没看完2章,而且看得也是一知半解,实在是受不了了,...

  • GroovyHelp 3.2.7 GA公布

    GroovyHelp 3.2.7 GA发布 GroovyHelp简介   GroovyHelp是一款Javadoc及Groovydoc搜索查阅软件,它能够帮助Java开发人员以...

  • Velocity在Roller中的使用

    Velocity在Roller中的应用 Velocity是java世界中出现比较早,也比较成熟的、性能比较好的、应用也比较广泛的模板框架。   所...

  • Rpc远程调用框架的设计与兑现(2)

    Rpc远程调用框架的设计与实现(2) 接上: 3   基于Json的前后端数据交互 3.1   轻量级的数据交换形式 3.1.1    什么是Jso...

  • excel 单元格的锁定 以及 JXL的兑现方式

    excel 单元格的锁定 以及 JXL的实现方式 在使用excel表格时,有些列是不希望用户可以修改的,诸如审计日志里面确定的部分,而审计...

  • 仓秤跟散料秤:java连接opc Server

    仓秤和散料秤:java连接opc Server 这三篇都是之前写好的,一直没发。 这次一起发出来吧。   java连接硬件很痛苦,特别是对我这...

  • Rpc远程调用框架的设计与兑现(1)

    Rpc远程调用框架的设计与实现(1) Rpc远程调用框架的设计与实现 1     Rpc远程调用框架设计概述 1.1   研究背景 1.1.1...

  • 集合中的线程安全有关问题

    集合中的线程安全问题 一、why? Java中常用的集合框架推荐使用的三个实现:HashSet\ArrayList\HashMap都是线程不安全的.如...

  • Java定时任务的兑现

    Java定时任务的实现 本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task)。 MyListener: ...

  • java中log日记的使用

    java中log日志的使用 一、介绍  Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控...

热门推荐: