SWT如何关闭窗口

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:01:56
public class window1 {

public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//

shell.open();

final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent arg0) {
//里该怎么写才能关闭这个窗口,用System。exit()不行啊,
而且需要关闭窗口后打开窗口window2.
}
});
button.setText("button");
button.setBounds(65, 121, 130, 56);
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

}

System。exit()这个肯定不行,他会把当前的运行的线程直接杀掉,肯定不能再打开新窗口了。 你这个shell有关闭的方法吧,把他关了,再钱打开一个新的就好了。

button 的监听是ActionListener而不是SelectionListener。

Window1:

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

public class Window1 extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton jButton1;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Window1 inst = new Window1();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public Window1() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.D