📄 button1.java
字号:
package cn.com.chengang.swt;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Button1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
final Button radio1 = new Button(shell, SWT.RADIO);// 事件代码里要访问radio1,所以加一个final
radio1.setText("男");// 设置按钮上的文字
radio1.setSelection(true);// 设置按钮处于选择状态
radio1.setBounds(10, 10, 40, 25); // 设置按钮位置
final Button radio2 = new Button(shell, SWT.RADIO);
radio2.setText("女");
radio2.setBounds(10, 30, 40, 25);
final Button check1 = new Button(shell, SWT.CHECK);
check1.setText("旅游");
check1.setBounds(70, 10, 40, 25);
final Button check2 = new Button(shell, SWT.CHECK);
check2.setText("篮球");
check2.setBounds(70, 30, 40, 25);
Button okButton = new Button(shell, SWT.NONE);
okButton.setText("确定");
okButton.setBounds(10, 70, 100, 25);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String str = "小明,";
if (radio1.getSelection())// 判断按钮是否被选
str += radio1.getText();// 取得按钮上的文字
if (radio2.getSelection())
str += radio2.getText();
str += "。爱好:";
if (check1.getSelection())
str += check1.getText();
if (check2.getSelection())
str += check2.getText();
// 信息提示框的第一、二个参数为空值也是可以的
MessageDialog.openInformation(null, null, str);
}
});
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -