当前位置: 技术问答>java相关
怎么关闭一个窗体?
来源: 互联网 发布时间:2015-08-18
本文导语: 我同时打开了两个窗体Frame1和Frame2,在Frame2上设置了一个按钮,我应该怎么样关掉Frame2呢,不是隐藏,也不是退出工程? | 加上这个试试: protected void processWindowEvent(WindowEvent e) { ...
我同时打开了两个窗体Frame1和Frame2,在Frame2上设置了一个按钮,我应该怎么样关掉Frame2呢,不是隐藏,也不是退出工程?
|
加上这个试试:
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
dispose();
}
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
dispose();
}
}
|
/**
*恕我直言,这个问题实在是有点简单,我认为你应该踏踏实实地看点书
*本例子的运行环境:win2kpro;j2sdk1.3;
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo5 extends WindowAdapter implements ActionListener
{
JButton b1 = null;
JButton b2 = null;
JButton bt = null;
JFrame newF = null;
public EventDemo5()
{
JFrame f = new JFrame("EventDemo5");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(1,2));
b1 = new JButton("按我有声音喔");
b2 = new JButton("按我可开新窗口");
b1.addActionListener(this);
b2.addActionListener(this);
contentPane.add(b1);
contentPane.add(b2);
f.pack();
f.show();
f.addWindowListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
Toolkit.getDefaultToolkit().beep();
if(e.getSource()==b2)
{
newF = new JFrame("新窗口");
newF.setSize(200,200);
bt = new JButton("关闭新窗口");
bt.addActionListener(this);
newF.getContentPane().add(bt);
newF.show();
}
if(e.getSource()==bt)
newF.dispose();
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main(String args[])
{
new EventDemo5();
}
}
*恕我直言,这个问题实在是有点简单,我认为你应该踏踏实实地看点书
*本例子的运行环境:win2kpro;j2sdk1.3;
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo5 extends WindowAdapter implements ActionListener
{
JButton b1 = null;
JButton b2 = null;
JButton bt = null;
JFrame newF = null;
public EventDemo5()
{
JFrame f = new JFrame("EventDemo5");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(1,2));
b1 = new JButton("按我有声音喔");
b2 = new JButton("按我可开新窗口");
b1.addActionListener(this);
b2.addActionListener(this);
contentPane.add(b1);
contentPane.add(b2);
f.pack();
f.show();
f.addWindowListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
Toolkit.getDefaultToolkit().beep();
if(e.getSource()==b2)
{
newF = new JFrame("新窗口");
newF.setSize(200,200);
bt = new JButton("关闭新窗口");
bt.addActionListener(this);
newF.getContentPane().add(bt);
newF.show();
}
if(e.getSource()==bt)
newF.dispose();
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main(String args[])
{
new EventDemo5();
}
}