如何在x秒内关闭Java GUI应用程序?
我需要一些关于我使用NetBeans开发的javagui的帮助。我想要功能如何在x秒内关闭Java GUI应用程序?,java,swing,user-interface,timer,Java,Swing,User Interface,Timer,我需要一些关于我使用NetBeans开发的javagui的帮助。我想要功能 用户单击按钮退出应用程序的位置。我不想立即退出,而是想弹出一条消息说“您的工作已提交,此窗口将在10秒后关闭”。并且可能显示从10到0的倒计时。有关如何实现Java对话的信息,请查看教程。如果要在关闭应用程序之前执行某些任务,可以在对话窗口中显示。有关如何实现Java对话的信息,请查看教程。如果您想在关闭应用程序之前执行某些任务,您可以在对话窗口中显示。是一个使用的示例。是一个使用的示例。以下是代码:(感谢Ko Wey
用户单击按钮退出应用程序的位置。我不想立即退出,而是想弹出一条消息说“您的工作已提交,此窗口将在10秒后关闭”。并且可能显示从10到0的倒计时。有关如何实现Java对话的信息,请查看教程。如果要在关闭应用程序之前执行某些任务,可以在对话窗口中显示。有关如何实现Java对话的信息,请查看教程。如果您想在关闭应用程序之前执行某些任务,您可以在对话窗口中显示。是一个使用的示例。是一个使用的示例。以下是代码:(感谢Ko Wey at )
#//文件CustomDialog.java
#导入javax.swing.*;
#导入java.awt.*;
#导入java.awt.event.*;
#
#类CustomDialog扩展JDialog实现ActionListener,Runnable{
#
#私有JButton JButton_Yes=null;
#私有JButton JButton_NO=null;
#私有布尔OK=false;
#私有线程线程=null;
#私有整数秒=0;
#private final int max=30;//最大秒数
#
#公共自定义对话框(框架){
#超级(帧,真);//模态
#setDefaultCloseOperation(WindowConstants.DO_NOTHING__ON_CLOSE);
#
#Box hBox=Box.createHorizontalBox();
#
#jButton_Yes=新jButton(“Yes”);
#jButton_Yes.addActionListener(这个);
#
#jButton_NO=新jButton(“NO”);
#jButton_NO.addActionListener(此);
#
#JLabel JLabel=新的JLabel(“你好吗?”);
#
#Container=getContentPane();
#cont.setLayout(新的BoxLayout(cont,BoxLayout.Y_轴));
#续加(jLabel);
#hBox.add(Box.createGlue());
#hBox.add(jButton_Yes);
#hBox.add(按钮号);
#续加(hBox);
#
#包装();
#线程=新线程(此);
#thread.start();
#setVisible(真);
# }
#
#已执行的公共无效操作(操作事件e){
#如果(例如getSource()==jButton_Yes)
#OK=正确;
#如果(例如getSource()==jButton_NO)
#OK=假;
#setVisible(假);
# }
#
#public void run(){
#而(秒<最大值){
#秒++;
#设置标题(“正常?”+秒);
#试试{
#睡眠(1000);
#}catch(InterruptedException exc){
# };
# }
#setVisible(假);
# }
#
#公共布尔值isOk(){return OK;}
#
#公共静态void main(字符串[]args){//testing
#CustomDialog cd=新建CustomDialog(新建JFrame());
#System.out.println(cd.isOk());
#cd=null;
#系统出口(0);
# }
#
#}//end
以下是代码:(感谢Ko Wey at
)
#//文件CustomDialog.java
#导入javax.swing.*;
#导入java.awt.*;
#导入java.awt.event.*;
#
#类CustomDialog扩展JDialog实现ActionListener,Runnable{
#
#私有JButton JButton_Yes=null;
#私有JButton JButton_NO=null;
#私有布尔OK=false;
#私有线程线程=null;
#私有整数秒=0;
#private final int max=30;//最大秒数
#
#公共自定义对话框(框架){
#超级(帧,真);//模态
#setDefaultCloseOperation(WindowConstants.DO_NOTHING__ON_CLOSE);
#
#Box hBox=Box.createHorizontalBox();
#
#jButton_Yes=新jButton(“Yes”);
#jButton_Yes.addActionListener(这个);
#
#jButton_NO=新jButton(“NO”);
#jButton_NO.addActionListener(此);
#
#JLabel JLabel=新的JLabel(“你好吗?”);
#
#Container=getContentPane();
#cont.setLayout(新的BoxLayout(cont,BoxLayout.Y_轴));
#续加(jLabel);
#hBox.add(Box.createGlue());
#hBox.add(jButton_Yes);
#hBox.add(按钮号);
#续加(hBox);
#
#包装();
#线程=新线程(此);
#thread.start();
#setVisible(真);
# }
#
#已执行的公共无效操作(操作事件e){
#如果(例如getSource()==jButton_Yes)
#OK=正确;
#如果(例如getSource()==jButton_NO)
#OK=假;
#setVisible(假);
# }
#
#public void run(){
#而(秒<最大值){
#秒++;
#设置标题(“正常?”+秒);
#试试{
#睡眠(1000);
#}catch(InterruptedException exc){
# };
# }
#setVisible(假);
# }
#
#公共布尔值isOk(){return OK;}
#
#公共静态void main(字符串[]args){//testing
#CustomDialog cd=新建CustomDialog(新建JFrame());
#System.out.println(cd.isOk());
#cd=null;
#系统出口(0);
# }
#
#}//end
除了烦扰用户之外,还有其他原因吗?我已经查看了以下代码:long test=System.currentTimeMillis();如果(test>=(pastTime+15*1000)){//乘以1000得到毫秒doSomething();}javax.swing.Timer出现在我的脑海中(在类似的情况下对我很有效)。你试过什么?编辑:不要使用System.currentTimeMillis方法。同样,请使用。谢谢大家的评论。下面是我问题的答案。希望它也能帮助其他人。小伙子们振作起来,乐于助人……总有一天你会需要同样的帮助。。!除了烦扰用户之外,还有其他原因吗?我已经查看了以下代码:longtest=System.currentTimeMillis();如果(test>=(pastTime+15*1000)){//乘以1000得到毫秒doSomething();}javax.swing.Timer出现在我的脑海中(在类似的情况下对我很有效)。你试过什么?编辑:不要使用System.currentTimeMillis方法。同样,请使用。谢谢大家的评论。下面是我问题的答案。希望它也能帮助其他人。小伙子们振作起来,乐于助人……总有一天你会需要同样的帮助。。!
# //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