栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在java / swing中关闭Windows时应采取的正确措施是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在java / swing中关闭Windows时应采取的正确措施是什么?

您可以使用的

setDefaultCloseOperation()
方法
JDialog
,指定
DISPOSE_ON_CLOSE

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

另请参见12.8程序退出。

附录:包含@camickr的有用答案,当关闭窗口或按下关闭按钮时,此示例退出。

import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.WindowEvent;import javax.swing.AbstractAction;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;public class DialogClose extends JDialog {    public DialogClose() {        this.setLayout(new GridLayout(0, 1));        this.add(new JLabel("Dialog close test.", JLabel.CENTER));        this.add(new JButton(new AbstractAction("Close") { @Override public void actionPerformed(ActionEvent e) {     DialogClose.this.setVisible(false);     DialogClose.this.dispatchEvent(new WindowEvent(         DialogClose.this, WindowEvent.WINDOW_CLOSING)); }        }));    }    private void display() {        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        this.pack();        this.setLocationRelativeTo(null);        this.setVisible(true);    }    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     new DialogClose().display(); }        });    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号