添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
欢乐的蚂蚁  ·  python ...·  1 年前    · 
霸气的酸菜鱼  ·  基础CherryPy - 知乎·  1 年前    · 
我有一个JAVA程序,里面有3个JFrame,每一个JFrame里面都有一个关闭按钮,点击本个里面的关闭按钮只关闭本JFrame,而对其他两个没有影响,我应该怎么写代码。... 我有一个JAVA程序,里面有3个JFrame,每一个JFrame里面都有一个关闭按钮,点击本个里面的关闭按钮只关闭本JFrame,而对其他两个没有影响,我应该怎么写代码。
import javax.swing.JFrame;

public class FrameTest extends JFrame {

public static void main(String[] args) {
new FrameTest("frame 1");
new FrameTest("frame 2");
new FrameTest("frame 3");
}

public FrameTest(String title) {
this.setTitle(title);
this.setSize(800, 600);
// this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// this.setDefaultCloseOperation(HIDE_ON_CLOSE);
// this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setVisible(true);
}

}

只要在每个Frame里设定this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);即可。

EXIT_ON_CLOSE,关闭程序。(所有窗口和进程都会关闭)

DISPOSE_ON_CLOSE,只关闭本窗口。

HIDE_ON_CLOSE,只隐藏本窗口,不关闭。

DO_NOTHING_ON_CLOSE,不做任何事,点击关闭无效。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class FrameTest extends JFrame {

public static void main(String[] args) {
new FrameTest("frame 1");
}

public FrameTest(String title) {
this.setTitle(title);
this.setSize(800, 600);
this.setLayout(new FlowLayout());
initPanel();
this.setVisible(true);
}

private void initPanel() {
JButton button = new JButton("关闭");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
this.add(button);
}

}

这很简单,只要调用dispose方法即可。隐藏是setVisible(false)。关闭程序是System.exit(0);