我的思路是这样的:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;public class FatherFrame extends JFrame {
JButton button;
public FatherFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
button=new JButton("弹出子窗口");
ButtonHandler handler=new ButtonHandler();
button.addActionListener(handler);
getContentPane().add(button);
setLocation(300,300);
pack();
} public static void main(String[] args) {
TestPanels tp = new TestPanels();
tp.show();
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
new SubFrame();
}
}} *********************************************import javax.swing.*;
import java.awt.*; public class SubFrame extends JFrame { public SubFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(new JLabel("这是弹出来的子窗口"));
setLocation(400,300);
pack();
show();
}
} 上面的两个源文件,按下父窗口的按钮后,能顺利的调出子窗口,但是,问题是,关闭子窗口时,父窗口也一起关掉了。请问,怎么样在关闭子窗口的时候,仍然保留父窗口。是不是我的思路根本就是错的,不能这样编。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;public class FatherFrame extends JFrame {
JButton button;
public FatherFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
button=new JButton("弹出子窗口");
ButtonHandler handler=new ButtonHandler();
button.addActionListener(handler);
getContentPane().add(button);
setLocation(300,300);
pack();
} public static void main(String[] args) {
TestPanels tp = new TestPanels();
tp.show();
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
new SubFrame();
}
}} *********************************************import javax.swing.*;
import java.awt.*; public class SubFrame extends JFrame { public SubFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(new JLabel("这是弹出来的子窗口"));
setLocation(400,300);
pack();
show();
}
} 上面的两个源文件,按下父窗口的按钮后,能顺利的调出子窗口,但是,问题是,关闭子窗口时,父窗口也一起关掉了。请问,怎么样在关闭子窗口的时候,仍然保留父窗口。是不是我的思路根本就是错的,不能这样编。
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;public class FatherFrame extends JFrame {
JButton button;
public FatherFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);button=new JButton("弹出子窗口");
ButtonHandler handler=new ButtonHandler();
button.addActionListener(handler);getContentPane().add(button);setLocation(300,300);
pack();
} public static void main(String[] args) {
FatherFrame tp = new FatherFrame();
tp.show();
} private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
new SubDialog(FatherFrame.this);
} }}class SubDialog extends JDialog
{
public SubDialog(JFrame owner)
{
super(owner,"SubDialog",true);
getContentPane().add(new JLabel("这是弹出来的子窗口"));setLocation(400,300);
pack();
show();
}
}
弹出窗口最好用JDialog来做,这样就可以达到楼主你的要求了
public void Father(){} main...{
JFrame jf= new JFrame();
....
JButton jb..
....
jb 的 action实现{
son sonFrame = new son()
son.newFrame();
} }
}
class son {
void son(){} void newFrame(){
JFrame xxx = new JFrame();
....
}
}分别再给两个类的关闭事件加dispose()销毁
----------------------------------------------------------
我认为不应该让某个类 xxxx.class extends JFrame ,然后为了创建一个窗体就要生成xxxx,关闭的时候让xxxx与这个窗体同生死共存亡.xxxx 经常可以包括一些有用的其他东西,窗体表现只该作为它的一个属性,需要出现的时候再出现,而不是一生成对象马上就出现
public void windowClosing(WindowEvent e) {
dispose();
}
});
在子窗口的构造方法里加这几句试试