SWT –按钮示例

本文详细介绍了SWT中五种不同样式的按钮:普通按钮、箭头按钮、切换按钮、复选框按钮和单选按钮,并通过代码示例展示了如何在SWT应用程序中创建和使用这些按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在SWT中,按钮小部件由五个按钮样式组成。

1)普通按钮 – SWT.PUSH
2)箭头按钮 – SWT.ARROW
3)切换按钮 – SWT.TOGGLE
4)复选框按钮 – SWT.CHECK
5)单选按钮 – SWT.RADIO

在这里,我演示了如何在SWT中创建这些按钮


import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class SWTButton {
 
public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
 
	//push button
	Button pushButton = new Button(shell, SWT.PUSH);
	pushButton.setLocation(50, 50);
	pushButton.setText("Im a Push Button");
	pushButton.pack();
 
	//arrow button
	Button arrowLeftButton = new Button(shell, SWT.ARROW | SWT.LEFT);
	arrowLeftButton.setLocation(50, 100);
	arrowLeftButton.setText("LEFT");
	arrowLeftButton.pack();
	
	Button arrowRightButton = new Button(shell, SWT.ARROW | SWT.RIGHT);
	arrowRightButton.setLocation(80, 100);
	arrowRightButton.setText("RIGHT");
	arrowRightButton.pack();
	
	Button arrowUpButton = new Button(shell, SWT.ARROW | SWT.UP);
	arrowUpButton.setLocation(110, 100);
	arrowUpButton.setText("UP");
	arrowUpButton.pack();
	
	Button arrowDownButton = new Button(shell, SWT.ARROW | SWT.DOWN);
	arrowDownButton.setLocation(140, 100);
	arrowDownButton.setText("DOWN");
	arrowDownButton.pack();
	
	//toggle
	Button toggleNotPressedButton = new Button(shell, SWT.TOGGLE);
	toggleNotPressedButton.setSelection(false);
	toggleNotPressedButton.setLocation(50, 150);
	toggleNotPressedButton.setText("Button Not Pressed");
	toggleNotPressedButton.pack();
	
	Button togglePressedButton = new Button(shell, SWT.TOGGLE);
	togglePressedButton.setSelection(true);
	togglePressedButton.setLocation(170, 150);
	togglePressedButton.setText("Button Pressed");
	togglePressedButton.pack();
	
	//check box
	Button[] checkBoxs = new Button[3];
	checkBoxs[0] = new Button(shell, SWT.CHECK);
	checkBoxs[0].setSelection(true);
	checkBoxs[0].setText("Choice 1");
	checkBoxs[0].setLocation(50,200);
	checkBoxs[0].pack();
	
	checkBoxs[1] = new Button(shell, SWT.CHECK);
	checkBoxs[1].setText("Choice 2");
	checkBoxs[1].setLocation(120,200);
	checkBoxs[1].pack();
	
	checkBoxs[2] = new Button(shell, SWT.CHECK);
	checkBoxs[2].setText("Choice 3");
	checkBoxs[2].setLocation(190,200);
	checkBoxs[2].pack();
	
	//radio
	Button[] radioButtons = new Button[3];
	radioButtons[0] = new Button(shell, SWT.RADIO);
	radioButtons[0].setSelection(true);
	radioButtons[0].setText("Choice 1");
	radioButtons[0].setLocation(50,250);
	radioButtons[0].pack();
	
	radioButtons[1] = new Button(shell, SWT.RADIO);
	radioButtons[1].setText("Choice 2");
	radioButtons[1].setLocation(120,250);
	radioButtons[1].pack();
	
	radioButtons[2] = new Button(shell, SWT.RADIO);
	radioButtons[2].setText("Choice 3");
	radioButtons[2].setLocation(190,250);
	radioButtons[2].pack();
	
	
	shell.setSize(500,500);
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

翻译自: https://mkyong.com/swt/swt-button-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值