JAVA.SWT/JFace: SWT基本组件之按钮(Button)

《Eclipse SWT/JFACE 核心应用》 清华大学出版社 5.2 按钮(Button)

    按钮有普通按钮(SWT.PUSH)、单选按钮(SWT.RADIO)、多选按钮(SWT.CHECK)、箭头按钮(SWT.ARROW)和切换按钮(SWT.TOGGLE)几种类型。
    同时,也可以设置按钮的样式。设置按钮文字对齐的样式有SWT.LEFT、SWT.RIGHT和SWT.CENTER。设置按钮的外观风格的样式有SWT.FLAT和SWT.BORDER。

1. 普通按钮(SWT.PUSH)

package www.swt.com;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class testButton {

/**
* @param args
*/
public static void main(String[] args) {

   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setSize(600, 100);
   shell.setLayout(new RowLayout(SWT.HORIZONTAL));
   shell.setText("Button");
  
   Button bt1 = new Button(shell, SWT.PUSH | SWT.LEFT);
   bt1.setText("SWT.LEFT");
   bt1.setToolTipText("SWT.LEFT"); //悬浮提示
  
   Button bt2 = new Button(shell, SWT.PUSH | SWT.RIGHT);
   bt2.setText("SWT.RIGHT");
   bt2.setToolTipText("SWT.RIGHT");
  
   Button bt3 = new Button(shell, SWT.PUSH | SWT.CENTER);
   bt3.setText("SWT.CENTER");
   bt3.setToolTipText("SWT.CENTER");
  
   Button bt4 = new Button(shell, SWT.PUSH | SWT.FLAT);
   bt4.setText("SWT.FLAT");
   bt4.setToolTipText("SWT.FLAT");
  
   Button bt5 = new Button(shell, SWT.PUSH | SWT.BORDER);
   bt5.setText("SWT.BORDER");
   bt5.setToolTipText("SWT.BORDER");
  
//   bt1.pack();
//   bt2.pack();
//   bt3.pack();
//   bt4.pack();
//   bt5.pack();

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

}
显示效果:

2. 切换按钮(SWT.TOGGLE)

切换按钮单击后保持按下状态,再次单击时恢复初始状态。
   Button bt1 = new Button(shell, SWT.TOGGLE | SWT.LEFT);
   bt1.setText("SWT.LEFT");
   bt1.setToolTipText("SWT.LEFT"); //悬浮提示
  
   Button bt2 = new Button(shell, SWT.TOGGLE | SWT.FLAT);
   bt2.setText("SWT.FLAT");
   bt2.setToolTipText("SWT.FLAT");
  
   Button bt3 = new Button(shell, SWT.PUSH | SWT.BORDER);
   bt3.setText("SWT.BORDER");
   bt3.setToolTipText("SWT.BORDER");
显示效果:

3. 箭头按钮(SWT.ARROW)

    箭头按钮是带有小箭头的按钮,创建时需要指定箭头的方向SWT.UP、SWT.DOWN、SWT.LEFT和SWT.RIGHT,若不指定方向,默认为SWT.UP。
   Button bt1 = new Button(shell, SWT.ARROW | SWT.UP);
   Button bt2 = new Button(shell, SWT.ARROW | SWT.DOWN);
   Button bt3 = new Button(shell, SWT.ARROW | SWT.LEFT);
   Button bt4 = new Button(shell, SWT.ARROW | SWT.RIGHT);
   Button bt5 = new Button(shell, SWT.ARROW | SWT.FLAT);
   Button bt6 = new Button(shell, SWT.ARROW | SWT.BORDER);
显示效果:

4. 单选按钮(SWT.RADIO)

   Group group1 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
   group1.setLayout(new FillLayout(SWT.VERTICAL));
   group1.setText("这是一组样式");
  
   Button bt1 = new Button(group1, SWT.RADIO | SWT.LEFT);
   bt1.setText("SWT.LEFT");
   Button bt2 = new Button(group1, SWT.RADIO | SWT.RIGHT);
   bt2.setText("SWT.RIGHT");
   Button bt3 = new Button(group1, SWT.RADIO | SWT.CENTER);
   bt3.setText("SWT.CENTER");
  
   //
   Group group2 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
   group2.setLayout(new FillLayout(SWT.VERTICAL));
   group2.setText("这是另一组样式");
  
   Button bt4 = new Button(group2, SWT.RADIO | SWT.FLAT);
   bt4.setText("SWT.FLAT");
   bt4.setSelection(true);
   Button bt5 = new Button(group2, SWT.RADIO | SWT.BORDER);
   bt5.setText("SWT.BORDER");
   Button bt6 = new Button(group2, SWT.RADIO);
   bt6.setText("SWT.RADIO");
对于单选按钮,创建对象时要指定按钮所属的父类。

显示效果:

   group2.setVisible(false); 显示效果:

5. 多选按钮(SWT.CHECK)

    Group group = new Group(shell, SWT.SHADOW_ETCHED_OUT);
    group.setLayout(new FillLayout(SWT.VERTICAL));
    group.setText("不同样式的多选按钮");
  
   Button bt1 = new Button(group, SWT.CHECK | SWT.LEFT);
   bt1.setText("SWT.LEFT");
   Button bt2 = new Button(group, SWT.CHECK | SWT.RIGHT);
   bt2.setText("SWT.RIGHT");
   Button bt3 = new Button(group, SWT.CHECK | SWT.CENTER);
   bt3.setText("SWT.CENTER");
   Button bt4 = new Button(group, SWT.CHECK | SWT.FLAT);
   bt4.setText("SWT.FLAT");
   Button bt5 = new Button(group, SWT.CHECK | SWT.BORDER);
   bt5.setText("SWT.BORDER");
显示效果:


增加事件:
   RowLayout layout = new RowLayout(SWT.VERTICAL);
   layout.marginWidth = 10;
   shell.setLayout(layout);
  
   Group group = new Group(shell, SWT.SHADOW_ETCHED_OUT);
   group.setLayout(new FillLayout(SWT.VERTICAL));
   group.setText("您的爱好是:");
  
   final Button[] button = new Button[5];
   String[] items = {"看书", "游泳", "音乐", "睡觉", "逛街"};
  
   for (int i = 0; i < button.length; i++) {
    button[i] =new Button(group, SWT.CHECK);
    button[i].setText(items[i]);
   }
  
   Button ok = new Button(group, SWT.PUSH);
   ok.setText("确定");

   ok.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
     for (int i = 0; i < button.length; i++) {
      if (button[i].getSelection()) {
       System.out.println("您选择了:"+button[i].getText());
      }
     }
    }
   });

显示效果:

6. 常用的方法

设置和获取按钮文本:setText、getText
设置和获取按钮是否被选中:setSelection、getSelection
设置和获取按钮图标:setImage、getImage
button.setImage(display.getSystemImage(SWT.ICON_ERROR))
button.setImage(new Image(display, "F:\\edit.gif"))
设置和获取按钮文字对齐方式:setAlignment、getAlignment

添加代码:
   ok.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
显示效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值