public CloseOtherFrame() { frame = new JFrame("CloseFrame"); firstbtn = new JButton("First Frame"); secondbtn = new JButton("Second Frame"); firstbtn.addActionListener(this); secondbtn.addActionListener(this);
public void actionPerformed(ActionEvent event) { if (event.getSource().equals(firstbtn)) { if (fd!=null) { if (!fd.isVisible()) fd.setVisible(true); return; } fd = new JDialog(frame); fd.setTitle("FirstDialog"); JButton b = new JButton("CloseSecondDialog"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (sd!=null) { sd.dispose(); } } }); fd.getContentPane().add(b); fd.pack(); fd.setVisible(true); }
if (event.getSource().equals(secondbtn)) { if(sd!=null) { if (!sd.isVisible()) sd.setVisible(true); return; } sd = new JDialog(frame); sd.setTitle("SecondDialog"); sd.setSize(100,100); sd.setVisible(true); } }
public static void main(String[] args) { new CloseOtherFrame(); } }
import java.awt.event.*;public class DialogTest extends Frame {
private Dialog d1;
private Dialog d2;
private Button b1;
private Button b2; public DialogTest() {
super("DialogTest");
this.setBounds(100, 100, 600, 400);
this.setVisible(true);
d1 = new Dialog(this, "Dialog1"); //无模式的 Dialog
d1.setLayout(new BorderLayout());
d1.setBounds(150, 150, 500, 400);
d2 = new Dialog(this,"Dialog2");
d2.setBounds(650,150,300,200);
b1 = new Button("NEW");
setLayout(new BorderLayout());
this.add(b1);
b1.setBounds(250,150,100,55);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
d2.setVisible(true);
}
}); b2 = new Button("close");
d1.add(b2,BorderLayout.SOUTH);
b2.setSize(20,10);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d2.setVisible(false);
}
}); this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String[] args) {
new DialogTest();
}
}仅供参考
this.addWindowListener(new WindowAdapter() {
@Override // 加上这句, 更保险一些
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(false)设置frame的窗口关闭属性(隐藏还是释放)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这样应该也可以吧
调用这个方法,使用System.exit(0);的话要把所有的全部关闭了
package je.test;
//通过一个窗体打开两个窗体,通过其他一个关闭另一个窗体
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;public class CloseOtherFrame implements ActionListener
{
JFrame frame;
JDialog fd,sd;
JButton firstbtn;
JButton secondbtn;
public CloseOtherFrame()
{
frame = new JFrame("CloseFrame");
firstbtn = new JButton("First Frame");
secondbtn = new JButton("Second Frame");
firstbtn.addActionListener(this);
secondbtn.addActionListener(this);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(firstbtn);
frame.getContentPane().add(secondbtn);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource().equals(firstbtn))
{
if (fd!=null)
{
if (!fd.isVisible()) fd.setVisible(true);
return;
}
fd = new JDialog(frame);
fd.setTitle("FirstDialog");
JButton b = new JButton("CloseSecondDialog");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (sd!=null)
{
sd.dispose();
}
}
});
fd.getContentPane().add(b);
fd.pack();
fd.setVisible(true);
}
if (event.getSource().equals(secondbtn))
{
if(sd!=null)
{
if (!sd.isVisible()) sd.setVisible(true);
return;
}
sd = new JDialog(frame);
sd.setTitle("SecondDialog");
sd.setSize(100,100);
sd.setVisible(true);
}
}
public static void main(String[] args)
{
new CloseOtherFrame();
}
}