就像下面这个简单的例子,子窗口关闭,父窗口也关闭,求解,怎么样才能让父窗口不关闭!!!谢谢
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
public class ParentFrame extends JFrame implements ActionListener{
private JButton jb = new JButton("显示子窗口");
public ParentFrame() {
super("父窗口");
this.add(jb);
jb.addActionListener(this);
this.setBounds(100, 100, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == jb) {
new ChildFrame();
}
}
public static void main(String[] args) {
new ParentFrame();
}
}
//子窗口
import javax.swing.JFrame;
public class ChildFrame extends JFrame {
public ChildFrame() {
super("子窗口");
this.setBounds(200, 200, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ChildFrame();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
public class ParentFrame extends JFrame implements ActionListener{
private JButton jb = new JButton("显示子窗口");
public ParentFrame() {
super("父窗口");
this.add(jb);
jb.addActionListener(this);
this.setBounds(100, 100, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == jb) {
new ChildFrame();
}
}
public static void main(String[] args) {
new ParentFrame();
}
}
//子窗口
import javax.swing.JFrame;
public class ChildFrame extends JFrame {
public ChildFrame() {
super("子窗口");
this.setBounds(200, 200, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ChildFrame();
}
}
改成 HIDE—ON—CLOSE或DISPOSE—ON—CLOSE
super("子窗口"); this.setBounds(200, 200, 200, 300);
this.setVisible(true);
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}子窗口修改下就行
这里改成HIDE_ON_CLOSE做为参数表示:调用任意已注册的 WindowListener 对象后自动隐藏该窗体。就可以了。
package AWTTest;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class ParentFrame extends JFrame implements ActionListener {
private JButton jb = new JButton("显示子窗口"); public ParentFrame() {
super("父窗口");
this.add(jb);
jb.addActionListener(this);
this.setBounds(100, 100, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
} @Override
public void actionPerformed(ActionEvent e) {
new ChildFrame();
} public static void main(String[] args) {
new ParentFrame();
}
}
package AWTTest;import javax.swing.JFrame;public class ChildFrame extends JFrame {
public ChildFrame() {
super("子窗口");
this.setBounds(200, 200, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
} public static void main(String[] args) {
new ChildFrame();
}}