日期:2014-05-20  浏览次数:20614 次

我遇到个JAVA桌面程序窗口问题 谁来帮帮我
我在主窗口中有个按钮,按钮有个点击事件,当我点按钮事,按钮给我产生另外一个窗口,是NEW出来的。但有个问题当我关闭这个窗口时,主窗口会随之关闭,有什么办法解决?谢谢(关闭窗口是点右上脚那个叉)

------解决方案--------------------
你是怎么处理了关闭打开新窗口?
------解决方案--------------------
估计你new出来的新窗口里有setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);了,或者是做了什么WindowsClose的监听。
如果有上述的设置,关闭窗口时就退出程序了。


------解决方案--------------------
用hide()
------解决方案--------------------
要不什么也不用好了
------解决方案--------------------
2楼阿宝说的对,肯定是你在关心窗口的事件中用了推出虚拟机的代码
应该调用dispose()来关闭窗口
------解决方案--------------------
package net.xiaohai;
/**
 * @author xiaohai
 *
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestMain extends JFrame {
private JPanel panel = null;
private JButton button=null;
private JFrame frame=null;
public TestMain() {
super("tital");
button=new JButton("弹出新窗体");
frame=new JFrame("new frame");
frame.setSize(400,300);
button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
}});
panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 350);
this.setLocationRelativeTo(null);
this.setVisible(true);
}

public static void main(String[] args) {
new TestMain();
}
}