我碰见的有两种情况子窗口关闭导致父窗口也关闭!下面简单介绍一下。。
一种是常规的,java原装类库引起的最常见的:
[java] view plain copy
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()
}
}
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()
}
}
这种情况是setDefaultCloseOperation()参数选择的问题。
EXIT_ON_CLOSE做参数的时候使用的是System.exit方法退出应用程序。故会关闭所有窗口。
而HIDE_ON_CLOSE参数表示调用已注册的WindowListener对象后自动隐藏窗体。
第二种是用地方放类库jfreeChart,在做图标的时候出现的。下面举例
[java] view plain copy
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()
}
}
import javax.swing.JFrame
public class ChildFrame extends ApplicationFrame {
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()
}
}
注意第二种情况,不管怎么setDefaultCloseOperation都会全关闭,因为子窗口是继承了ApplicationFrame即整个应用。故所有父窗口都会关闭。方法一:类 JFrame
javax.swing.JFrame
JFrame中的方法void setDefaultCloseOperation(int)可以设置
以下为改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起
"close" 时默认执行的操作。必须指定以下选项之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的
WindowListener 对象的 windowClosing 方法中处理该操作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener
对象后自动隐藏该窗体。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener
的对象后自动隐藏并释放该窗体。
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit
方法退出应用程序。仅在应用程序中使用。
默认情况下,该值被设置为 HIDE_ON_CLOSE。更改此属性的值将导致激发属性更改事件,其属性名称为
"defaultCloseOperation"。
注:当 Java 虚拟机 (VM) 中最后一个可显示窗口被释放后,虚拟机可能会终止
要实现你说的,应该采用
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
方法二:
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.JOptionPane
public class Test extends JFrame {
public Test(){
this.setTitle("title")
this.setSize(300,200)
this.setLocation(100,100)
//设置关闭时什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
//监听关闭按钮的点击操作
this.addWindowListener(new WindowAdapter(){
//new 一个WindowAdapter 类 重写windowClosing方法
//WindowAdapter是个适配器类 具体看jdk的帮助文档
public void windowClosing(WindowEvent e) {
//这里写对话框
if(JOptionPane.showConfirmDialog(null,
"退出","提
示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0)
}
}
})
this.setVisible(true)
}
public static void main(String[] args) {
new Test()
}
}
process.destroy()cmd
/c
dir
是执行完dir命令后封闭命令窗口。
cmd
/k
dir
是执行完dir命令后不封闭命令窗口。
cmd
/c
start
dir
会打开一个新窗口后执行dir指令,原窗口会封闭。
cmd
/k
start
dir
会打开一个新窗口后执行dir指令,原窗口不会封闭。