eclipse学习笔记!(7) ----- SWT Designer 下 其他SWT組件

  • 工具栏(ToolBar类、ToolItem类、ViewForm类)

        ViewForm是一个容器,它是编辑器的基座,用来容纳工具栏和文本框。ToolBar是一个工具栏,同时也是一个容器,即容纳工具栏的一个个按钮(ToolItem)。
        在写例子或做其他的时候,只要注意将ViewForm放入到shell上,然后将ToolBar放到ViewForm上,之后将ToolItem放在ToolBar上,页面的基本效果就出来了。下面写一段比较简单的代码:

         final  ViewForm viewForm  =   new  ViewForm(shell, SWT.BORDER);
        viewForm.setLayout(
new  FillLayout());
        
final  ToolBar toolBar  =   new  ToolBar(viewForm, SWT.NONE);
        
final  Text thisIsTestText  =   new  Text(viewForm, SWT.BORDER | SWT.V_SCROLL);
        thisIsTestText.setText(
" this is test! " );
        viewForm.setContent(thisIsTestText);
        viewForm.setTopLeft(toolBar);        
        
final  ToolItem newItemToolItem  =   new  ToolItem(toolBar, SWT.PUSH);
        newItemToolItem.addSelectionListener(
new  SelectionAdapter()  {
            
public void widgetSelected(SelectionEvent e) {
                String str 
= thisIsTestText.getText();
                MessageDialog.openInformation(
null,null,str);
            }

        }
);
        newItemToolItem.setText(
" show " );
        
final  ToolItem newItemToolItem_1  =   new  ToolItem(toolBar, SWT.PUSH);
        newItemToolItem_1.addSelectionListener(
new  SelectionAdapter()  {
            
public void widgetSelected(SelectionEvent e) {
                thisIsTestText.setText(
"");
            }

        }
);
        newItemToolItem_1.setText(
" hide " );

上面例子里面的Layout 是FillLayout 布局方式!

  • 动态工具栏(CoolBar类、CoolItem类)

        CoolBar组件是一个可以放置多个工具栏的容器,基于CoolBar做出的工具栏可以通过拖动操作改变摆放位置(office系列的工具栏类似的效果)。
        要做CoolBar工具栏必须是要CoolBar,CoolItem,ToolBar,ToolItem 四个一起使用才可以。其中ToolBar是建立在CoolBar容器里的,且CoolItem并非是用来显示按钮的,而是用来帮助CoolBar控制 ToolBar的。 这里的CoolBar,CoolItem,ToolBar和前面说过的TabFolder,TabItem,Group三者是一样的关系。实例代码如下:

public   class  Tabfolder1  {
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
500365);
        shell.setText(
"SWT Application");        
        shell.setLayout(
new FillLayout());
        
//同样需要用ViewForm来做底层的容器
        final ViewForm viewForm = new ViewForm(shell, SWT.NONE);
        viewForm.setLayout(
new FillLayout());
        
//在ViewForm上放CoolBar和Text
        CoolBar coolBar = new CoolBar(viewForm,SWT.NONE);
        Text text 
= new Text(viewForm,SWT.BORDER|SWT.V_SCROLL);    
        
//设置CoolBar和Text在ViewForm上的位置
        viewForm.setContent(text);
        viewForm.setTopLeft(coolBar);
        
/*
         * 为CoolBar添加2个工具栏,分别在2个大括号里面,括号本身没有意思,可以去掉
         
*/
       
        
{
            
final ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);
            
//为子工具栏添加2个按钮
            final ToolItem newItemToolItem = new ToolItem(toolBar, SWT.PUSH);
            newItemToolItem.setText(
"GET");
            newItemToolItem.setImage(
new Image(display,"src/275.jpg"));
            
final ToolItem newItemToolItem_1 = new ToolItem(toolBar, SWT.PUSH);
            newItemToolItem_1.setText(
"CLEAR");
            newItemToolItem_1.setImage(
new Image(display,"src/275.jpg"));
            
//创建一个CoolItem来控制此子工具栏
            final CoolItem newItemCoolItem = new CoolItem(coolBar, SWT.NONE);
            newItemCoolItem.setControl(toolBar);
            
//调整ToolBar为合适的尺寸,并用ToolBar的尺寸来调整CoolItem
            toolBar.pack();
            Point point 
= new Point(toolBar.getSize().x,toolBar.getSize().y);            
            newItemCoolItem.setSize(point);
            
//设定CoolItem的最小尺寸,如不设定,则按钮会呗其他子工具栏遮住
            newItemCoolItem.setMinimumSize(point);            
        }

        
{//同样的方式创建一个子工具栏2
            final ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);            
            
final CoolItem newItemCoolItem = new CoolItem(coolBar, SWT.NONE);
            newItemCoolItem.setControl(toolBar);            
            
final ToolItem newItemToolItem = new ToolItem(toolBar, SWT.PUSH);
            newItemToolItem.setImage(
new Image(display,"src/275.jpg"));            
            toolBar.pack();
            Point point 
= new Point(toolBar.getSize().x,toolBar.getSize().y);            
            newItemCoolItem.setSize(point);
            newItemCoolItem.setMinimumSize(point);            
        }

        shell.layout();
        
/*
         * 下面监听coolBar中子工具栏的改变,如有改变则重排viewForm中组件的位置。不加,则页面会出现重叠什么的
         * 若此监听写在shell.layout()的前面,则窗口打开时,此事件就会触发一次,完全没有必要。为优化代码,所以...
         
*/

        coolBar.addControlListener(
new ControlAdapter(){
           
public void controlResized(ControlEvent e){
               viewForm.layout();
           }

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

    }

}
  • 菜单(Menu类,MenuItem类)

        这个就不加代码了,比较的乱,看不清楚,要想比较清楚的了解,就必须自己动手了。个人感觉design比手写的要方便的多,比较快点(可能会有些冗余代码)。
        一般创建Menu对象的第一个参数是shell,
        创建MenuItem对象的第一个参数是MenuItem所在的Menu对象。
        如果Menu是某个MenuItem的子菜单,则还需要加一个设置:MenuItem.setMenu(Menu);
Menu的式样罗列如下:
        SWT.BAR ,用于主菜单
        SWT.DROP_DOWN , 用于子菜单
        SWT.POP_UP ,用于右键菜单(Windows桌面效果)
MenuItem的式样罗列如下:
        SWT.CHECK ,选择后前面会加一个小勾
        SWT.CASCADE ,有子菜单的
        SWT.PUSH , 普通型
        SWT.RADIO ,选择后,前面会显示一个圆点
        SWT.SEPARATOR ,分隔符(显示一个横线,把几个选项隔开)

  • 滑动条(slider)、刻度条(scale)、进度条(progressBar)
  1. 滑动条(slider类)
    下面代码显示结果:窗口里有2个组件,右边是一个滑动条,左边是一个用来显示滑动条当前值的文本框。
    ......
            shell.setLayout(
    new  RowLayout());
            
    final  Text text  =   new  Text(shell, SWT.BORDER);
            
    // 创建一个滑动条,默认是水平型,用样式SWT.VERTICAL 可变成垂直型
             final  Slider slider  =   new  Slider(shell, SWT.NONE);
            slider.setMinimum(
    0 );  // 最小值
            slider.setMaximum( 100 ); // 最大值
            slider.setIncrement( 5 ); // 滚动间隔值
            slider.setPageIncrement( 10 ); // 翻页间隔值
            slider.setSelection( 25 ); // 初始值
            slider.addSelectionListener( new  SelectionAdapter() {
               
    public void widgetSelected(SelectionEvent e){
                   text.setText(
    "" + slider.getSelection());
               }

            }
    );
    ......
  2. 刻度条(scale类)---像小尺的样式
    刻度条和滑动条在使用方法上是一样的。将上面代码中创建滑动条的语句修改如下,即可:
    // final Slider slider = new Slider(shell, SWT.NONE);
    final  Scale scale =   new  Scale(shell, SWT.NONE);
  3. 进度条(progressBar类)testCase的那种条条
    进度条的使用方法和上面的2个类似,主要也是3种方法,setMinimum,setMaximum,分别是最小值和最大值,setSelection方法控制进度条当前的位置。具体代码如下:
            shell.setLayout( new  RowLayout());        
            
    final  ProgressBar progressBar  =   new  ProgressBar(shell, SWT.SMOOTH);
            progressBar.setMinimum(
    0 );
            progressBar.setMaximum(
    100 );
            
    final  Button button  =   new  Button(shell, SWT.BORDER);
            button.setText(
    " GO " );
            button.addSelectionListener(
    new  SelectionAdapter() {
               
    public void widgetSelected(SelectionEvent e){
                   
    for(int i=1; i<11 ; i++){
                       
    try{
                           Thread.sleep(
    1000);//间隔1秒
                       }
    catch(Throwable e2){}
                       progressBar.setSelection(i
    *10);
                   }

               }

            }
    );

    程序说明:
    上面程序演示了进度条的使用,但有一个大毛病,就是当开始执行处理过程时,页面就很难在响应其他的操作了,解决这个问题就需要用线程了。
    另外JFace包里,有一种dialog组件“ProgressMonitorDialog”,它也有表示任务执行进度的功能。
  • 画布(canvas类)

        Canvas用于显示图像,既可以在Canvas上画图,也可以直接将图片显示于其中,通过下面的代码来演示Canvas使用的3个主要方面:设置图像、更换图像、清除图像。代码如下:

public   class  Tabfolder1  {
    
static Image image;    
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
502360);        
        shell.setText(
"SWT Application");
        shell.setLayout(
new RowLayout());        
        
final Canvas canvas = new Canvas(shell,SWT.BORDER);
        canvas.addPaintListener(
new PaintListener(){//监听canvas的重绘事件
            public void paintControl(final PaintEvent event) {//将其图像显示在canvas上
                if(image != null){
                    event.gc.drawImage(image, 
1010);//10,10是显示图像的左上角坐标
                }

            }

        }
);
        Button button1 
= new Button(shell,SWT.NONE);
        button1.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                image 
= new Image(display,"src/275.jpg");
                canvas.redraw();
            }

        }
);
        button1.setText(
"photo1");
        
        Button button2 
= new Button(shell,SWT.NONE);
        button2.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                image 
= new Image(display,"src/pre.gif");
                canvas.redraw();
            }

        }
);
        button2.setText(
"photo2");
        Button clearButton 
= new Button(shell,SWT.NONE);
        clearButton.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                
if(image != null){
                    image.dispose();
                    image 
= null;
                    canvas.redraw();
                }

            }

        }
);
        clearButton.setText(
"cleanPhoto");
        shell.layout();
        shell.open();        
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }

}
  • 表格(Table类)

        实际项目中,多是有关DB开发的,一般不会用Table,而用JFace中基于Table扩展的TableViewer来作为表格组件。 所以不多说了,随便看看下面代码实现下面的各功能:

public   class  Tabfolder1  {
    
static Image image;    
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
502360);        
        shell.setText(
"SWT Application");
        shell.setLayout(
new FillLayout()); 
        
final Table table = new Table(shell,SWT.MULTI|SWT.FULL_SELECTION);
        table.setHeaderVisible(
true);
        table.setLinesVisible(
true);        
        TableColumn col1 
= new TableColumn(table,SWT.NONE);
        col1.setText(
"ID");
        col1.setWidth(
80);
        TableColumn col2 
= new TableColumn(table,SWT.NONE);
        col2.setText(
"name");
        col2.setWidth(
80);        
        Composite composite 
= new Composite(shell,SWT.NONE);
        composite.setLayout(
new RowLayout());        
        Button addButton 
= new Button(composite,SWT.NONE);
        addButton.setText(
"add");
        addButton.addSelectionListener(
new SelectionAdapter() {
            
int i= 1;
            
public void widgetSelected(SelectionEvent e) {
                TableItem item 
= new TableItem(table,0);
                item.setText(
new String[]{""+i,"yjy"+i});
                i
++;
            }

        }
);        
        Button removeButton 
= new Button(composite,SWT.NONE);
        removeButton.setText(
"remove");
        removeButton.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                
int[] x = table.getSelectionIndices();
                table.remove(x);
            }

        }
);        
        table.addMouseListener(
new MouseListener(){
            
public void mouseDoubleClick(MouseEvent e) {
                
int x = table.getSelectionIndex();
                TableItem item 
= table.getItem(x);
                String str 
= "li one =" +item.getText(0+ "  li two = " + item.getText(1);
                MessageDialog.openInformation(
null,null,str);
            }

            
public void mouseDown(MouseEvent e) { }
            
public void mouseUp(MouseEvent e) { }
        }
);
        shell.layout();
        shell.open();        
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }

}
  • 数(tree类)

        和Table一样,JFace里也有基于Tree扩展的TreeViewer,实际用到数据库的项目都用它。还是简单介绍下:

        shell.setLayout( new  FillLayout());        
        
final  Tree tree  =   new  Tree(shell,SWT.SINGLE);
        TreeItem chinaItem 
=   new  TreeItem(tree,SWT.NONE);
        chinaItem.setText(
" china " );
            
new  TreeItem(chinaItem,SWT.NULL).setText( " beijing " );
            
new  TreeItem(chinaItem,SWT.NULL).setText( " taiwan " );
            
new  TreeItem(chinaItem,SWT.NULL).setText( " shanghai " );
        
new  TreeItem(tree,SWT.NONE).setText( " USA " );     
        tree.addSelectionListener(
new  SelectionAdapter() {//单击tree的节点时,节点的值显示在窗口标题栏中
            public void widgetSelected(SelectionEvent e){
                
//Tree也可以支持多选,所以getSelection返回的是数组,本例没有设置多选        
                TreeItem[] item = tree.getSelection();
                shell.setText(item[
0].getText());
            }

        }
);

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值