首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在x秒内关闭Java GUI应用程序?

如何在x秒内关闭Java GUI应用程序?
EN

Stack Overflow用户
提问于 2011-08-24 09:24:00
回答 3查看 1.6K关注 0票数 1

我需要一些关于我使用NetBeans开发的Java GUI的帮助。我想要的功能,用户点击一个按钮退出应用程序。而不是立即退出,我希望弹出一条消息说“您的作业已提交,此窗口将在10秒内关闭”。也许会显示从10到0的倒数。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-25 03:25:27

代码如下:(感谢http://www.coderanch.com/t/341814/GUI/java/JOptionPane-Timeout的Ko Wey )

代码语言:javascript
复制
# //file CustomDialog.java    
# import javax.swing.*;  
# import java.awt.*;  
# import java.awt.event.*;  
#    
# class CustomDialog extends JDialog implements ActionListener,Runnable{  
#    
# private JButton jButton_Yes = null;  
# private JButton jButton_NO = null;  
# private boolean OK = false;  
# private Thread thread = null;  
# private int seconds = 0;  
# private final int max = 30;//max number of seconds   
#    
# public CustomDialog(Frame frame){  
# super(frame,true);//modal  
# setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);  
#    
# Box hBox = Box.createHorizontalBox();  
#    
# jButton_Yes = new JButton("Yes");  
# jButton_Yes.addActionListener(this);  
#    
# jButton_NO = new JButton("NO");  
# jButton_NO.addActionListener(this);  
#    
# JLabel jLabel = new JLabel("How are you?");  
#    
# Container cont = getContentPane();  
# cont.setLayout(new BoxLayout(cont,BoxLayout.Y_AXIS));  
# cont.add(jLabel);  
# hBox.add(Box.createGlue());  
# hBox.add(jButton_Yes);  
# hBox.add(jButton_NO);  
# cont.add(hBox);  
#    
# pack();  
# thread = new Thread(this);  
# thread.start();  
# setVisible(true);  
# }  
#    
# public void actionPerformed(ActionEvent e){  
# if (e.getSource()==jButton_Yes)  
# OK = true;  
# if (e.getSource()==jButton_NO)  
# OK = false;  
# setVisible(false);  
# }  
#    
# public void run(){  
# while(seconds < max){  
# seconds++;  
# setTitle("OK? "+seconds);  
# try{  
# Thread.sleep(1000);  
# }catch (InterruptedException exc){  
# };  
# }  
# setVisible(false);  
# }  
#    
# public boolean isOk(){return OK;}  
#    
# public static void main(String[] args){//testing  
# CustomDialog cd = new CustomDialog(new JFrame());  
# System.out.println(cd.isOk());  
# cd = null;  
# System.exit(0);  
# }  
#    
# }//end  
票数 2
EN

Stack Overflow用户

发布于 2011-08-24 10:06:17

JOptionTest就是一个使用javax.swing.Timer的例子。

票数 3
EN

Stack Overflow用户

发布于 2011-08-24 09:30:42

有关如何实现Java对话的信息,请查看教程How to Make Dialogs。如果你想在关闭应用程序之前执行一些任务,你可以在你的对话窗口中显示progress

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7169463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档