SWT如何关闭窗口
publicclasswindow1{publicstaticvoidmain(String[]args){finalDisplaydisplay=Display.get...
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();
}
}
} 展开
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();
}
}
} 展开
展开全部
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.DISPOSE_ON_CLOSE);
this.setTitle("Window1");
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jButton1 = new JButton();
jPanel1.add(jButton1);
jButton1.setText("OK");
jButton1.setBounds(111, 62, 103, 22);
jButton1.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
Window1.this.setVisible(false);
Window1.this.dispose();
Window2 w2=new Window2();
w2.setVisible(true);
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Window2:
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class Window2 extends javax.swing.JFrame {
public Window2() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Window2");
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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.DISPOSE_ON_CLOSE);
this.setTitle("Window1");
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jButton1 = new JButton();
jPanel1.add(jButton1);
jButton1.setText("OK");
jButton1.setBounds(111, 62, 103, 22);
jButton1.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
Window1.this.setVisible(false);
Window1.this.dispose();
Window2 w2=new Window2();
w2.setVisible(true);
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Window2:
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class Window2 extends javax.swing.JFrame {
public Window2() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Window2");
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询