在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 ();
}
}
标签: swt
翻译自: https://mkyong.com/swt/swt-button-example/