📄 shell2.java
字号:
package cn.com.chengang.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Shell2 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("主窗口");
shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("打开新窗口");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.setVisible(false);// 隐藏主窗口
// 创建新窗口
final Shell newShell = new Shell(shell, SWT.CLOSE);
newShell.setText("新窗口");
newShell.setSize(300, 300);
newShell.setLayout(new RowLayout());
// 监听关闭事件。当新窗口关闭时显示主窗口
newShell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
if (!shell.isVisible())
shell.setVisible(true);
}
});
final Button visibleButton = new Button(newShell, SWT.NONE);
visibleButton.setText("显示/隐藏主窗口");
visibleButton.addSelectionListener(new SelectionAdapter() {
private boolean flag = true;
public void widgetSelected(final SelectionEvent e) {
shell.setVisible(flag);
flag = !flag;
// 因为shell是newShell的父容器,所以shell隐藏newShell也会跟着隐藏,因此要重新显示newShell
newShell.setVisible(true);
newShell.setActive();
}
});
newShell.open();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -