📄 helloworld2.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;
/**
* 4、HelloWorld更进一步
*/
public class HelloWorld2 {
public static void main(String[] args) {
// Display负责管理事件循环和控制UI线程和其他线程之间的通信
final Display display = Display.getDefault();
final Shell shell = new Shell(); // shell是程序的主窗口
shell.setSize(327, 253); // 设置主窗口的大小
shell.setText("Hello World"); // 设置主窗口的标题
// ---------创建窗口中的其他界面组件-------------
Button button = new Button(shell, SWT.NONE); // 创建一个按钮对象
button.setText("HelloWorld"); // 设置按钮上的文字
button.setBounds(88, 94, 100, 25); // 设置按钮在窗口中的位置和大小
// 编写按钮被单击时的事件代码
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// 弹出一个对话框,MessageDialog是JFace中的类
MessageDialog.openInformation(shell, "hello", "HelloWorld");
}
});
// -----------------END------------------------
shell.layout(); // 应用界面布局
shell.open(); // 打开shell主窗口
while (!shell.isDisposed()) { // 如果主窗口没有关闭,则一直循环
if (!display.readAndDispatch()) // 如果display不忙,就让display处在休眠状态
display.sleep();
}
display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -