Eclipse大本营


SWT开发 - Button的使用(1)

2008-01-23 00:28Update

eclipse

本文SWT Button(按钮控件)的一般用法。

范例
代码:ButtonSample01.java
package com.test.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ButtonSample01 {

    public static void main(String[] args) {
        Display display = new Display();
        Image image = display.getSystemImage(SWT.ICON_INFORMATION);
        Shell shell = new Shell(display);
        //shell.setLayout (new GridLayout());
        shell.setLayout (new FillLayout());
        Button button01 = new Button(shell, SWT.PUSH);
        button01.setImage(image);
        button01.setText("P");
        
        Button button02 = new Button(shell, SWT.PUSH | SWT.LEFT);
        button02.setText("L");
        
        Button button03 = new Button(shell, SWT.PUSH | SWT.CENTER);
        button03.setText("C");
        Button button04 = new Button(shell, SWT.PUSH | SWT.RIGHT);
        button04.setText("R");
        
        Button button05 = new Button(shell, SWT.TOGGLE);
        button05.setText("T");
        
        Button button06 = new Button(shell, SWT.ARROW);
        button06.setText("A");
        new Button(shell, SWT.ARROW | SWT.UP);
        new Button(shell, SWT.ARROW | SWT.RIGHT);
        new Button(shell, SWT.ARROW | SWT.DOWN);
        new Button(shell, SWT.ARROW | SWT.LEFT);
        
        
        Button button07 = new Button(shell, SWT.CHECK);
        button07.setText("C");
        
        Button button08 = new Button(shell, SWT.RADIO);
        button08.setText("R");
               
        
        
        shell.setSize(520, 70);
        shell.open();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }

}


说明:
Button的基本方法:
Button.setText(按钮上显示的文字);
Button.setImage(图像);

Button的基本风格:
SWT.PUSH:普通的按钮
SWT.TOGGLE:toggle按钮
SWT.CHECK:checkbox按钮
SWT.RADIO:radio按钮
SWT.ARROW:显示为箭头

SWT.LEFT:居左。可以设置按钮文字属性或箭头(SWT.ARROW)的居左属性。
SWT.CENTER:居中。
SWT.RIGHT:居右。可以设置按钮文字属性或箭头(SWT.ARROW)的居右属性。
SWT.UP:朝上。可以设置箭头(SWT.ARROW)的朝上属性。
SWT.DOWN:朝下。可以设置箭头(SWT.ARROW)的朝下属性。

编译后执行显示为:
 
Copyright ©2006-2010 lifevv.com. All Rights Reserved
POWERED BY @pmplat.syboos.com