Swt - Button

在Swt里 按钮的分类

普通按钮(SWT.PUSH)
单选按钮(SWT.RADIO)
多选按钮(SWT.CHECK)
箭头按钮(SWT.ARROW)
切换按钮(SWT.TOGGLE)

按钮的对齐方式样式

SWT.LEFT
SWT.RIGH
SWT.CENTER

按钮的外观风格的样式

SWT.FLAT
SWT.BORDER

普通按钮(SWT.PUSH) 例:

效果图

[img]http://dl.iteye.com/upload/attachment/563675/500baef1-9dde-3208-b145-c940a4d7b7f9.jpg[/img]

代码


package me.iace.swt.ch4;

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

public class HelloWorld {

public static void main(String[] args) {

Display display = Display.getDefault();

Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
shell.setText("Hello World");

Button bt1 = new Button(shell, SWT.PUSH|SWT.LEFT);
bt1.setText("PUSH LEFT");
bt1.pack();

Button bt2 = new Button(shell, SWT.PUSH|SWT.RIGHT);
bt2.setText("PUSH RIGHT");
bt2.pack();

Button bt3 = new Button(shell, SWT.PUSH|SWT.CENTER);
bt3.setText("PUSH CENTER");
bt3.pack();

Button bt4 = new Button(shell, SWT.PUSH|SWT.FLAT);
bt4.setText("PUSH FLAT");
bt4.pack();

Button bt5 = new Button(shell, SWT.PUSH|SWT.BORDER);
bt5.setText("PUSH LEFT");
bt5.pack();

shell.pack();
shell.open();

while(!shell.isDisposed()){

if(!display.readAndDispatch()){

display.sleep();
}

}

display.dispose();
}

}



切换按钮(SWT.TOGGLE) 例:
切换按钮与普通按钮类似,但是当单击后保持按下状态,再次单击时恢复初始状态。

效果图

[img]http://dl.iteye.com/upload/attachment/563681/ee0e8d35-1b58-32fd-a7ed-ef3cd6358737.jpg[/img]


代码

package me.iace.swt.ch4;

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

public class HelloWorld {

public static void main(String[] args) {

Display display = Display.getDefault();

Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
shell.setText("Hello World");

Button bt1 = new Button(shell, SWT.TOGGLE|SWT.LEFT);
bt1.setText("TOGGLE LEFT");
bt1.pack();

Button bt2 = new Button(shell, SWT.TOGGLE|SWT.FLAT);
bt2.setText("TOGGLE FLAT");
bt2.pack();

Button bt3 = new Button(shell, SWT.TOGGLE|SWT.BORDER);
bt3.setText("TOGGLE BORDER");
bt3.pack();


shell.pack();
shell.open();

while(!shell.isDisposed()){

if(!display.readAndDispatch()){

display.sleep();
}

}

display.dispose();
}

}



Radio 按钮

效果图

[img]http://dl.iteye.com/upload/attachment/563686/5b6a9d04-1de1-38ce-a2d2-d46ed2626772.jpg[/img]


代码


package me.iace.swt.ch4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;

public class ButtonRadioCase {

public static void main(String[] args) {

Display display = new Display();

Layout shellLayout = new FillLayout(SWT.HORIZONTAL);

Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(shellLayout);
shell.setText("Radio Button");

Layout groupLayout = new FillLayout(SWT.VERTICAL);

Group radioGroup1 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
radioGroup1.setLayout(groupLayout);
radioGroup1.setText("First Style");
radioGroup1.setToolTipText("First Style");

Button bt1 = new Button(radioGroup1, SWT.RADIO|SWT.LEFT);
bt1.setText("Radio LEFT");
bt1.setToolTipText("Radio LEFT");

Button bt2 = new Button(radioGroup1, SWT.RADIO|SWT.RIGHT);
bt2.setText("Radio RIGHT");
bt2.setToolTipText("Radio RIGHT");

Button bt3 = new Button(radioGroup1, SWT.RADIO|SWT.CENTER);
bt3.setText("Radio CENTER");
bt3.setToolTipText("Radio CENTER");

Group radioGroup2 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
radioGroup2.setLayout(groupLayout);
radioGroup2.setText("Second Style");

Button bt4 = new Button(radioGroup2, SWT.RADIO|SWT.FLAT);
bt4.setText("Radio FLAT");
bt4.setToolTipText("Radio FLAT");
bt4.setSelection(true);

Button bt5 = new Button(radioGroup2, SWT.RADIO|SWT.BORDER);
bt5.setText("Radio BORDER");
bt5.setToolTipText("Radio BORDER");

Button bt6 = new Button(radioGroup2, SWT.RADIO);
bt6.setText("Radio");
bt6.setToolTipText("Radio");

shell.pack();
shell.open();

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

display.dispose();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值