就像下面这个简单的例子,子窗口关闭,父窗口也关闭,求解,怎么样才能让父窗口不关闭!!!谢谢
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();
}
}
解决方案 »
- 两台机器socket连接的条件
- 1099端口正常 visualvm连接不上
- JTextArea初期表示小问题(20行代码)
- &和&&的区别?
- JAVA中如何访问另一个类的prviate方法 ?
- 下面代码段不理解
- thinking in java中bruce eckel讲到产生sql语句可以使用microsoft query,但是??
- ★我有一个鼠标点击放烟花的applet程序(java fan send me),但是我发现有一些问题,大家帮忙看看!
- 哪儿有powerdesign下载?
- java在读取sybase11时出现乱码,是什么原因,各位大哥,大姐,大虾们,拜托啦
- 关于异常与多态性
- 一个java小问题
改成 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();
}}