我需要一些关于使用NetBeans开发的Java GUI的帮助。我想要用户点击一个按钮退出应用程序的功能 。我不想立即退出,而是想弹出一条消息,说:“您的工作已提交,并且此窗口将在10秒内关闭”。并且可能显示从10倒数到0.如何在x秒内关闭Java GUI应用程序?
1
A
回答
2
下面是该代码:(在 http://www.coderanch.com/t/341814/GUI/java/JOptionPane-Timeout感谢柯卫)
# //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
1
有关如何实现Java对话的信息,请参阅教程How to Make Dialogs。如果您想在关闭应用程序之前执行一些任务,则可以在对话窗口中显示progress。
3
JOptionTest
是一个使用javax.swing.Timer
的示例。
相关问题
- 1. 在按一下按钮,关闭应用程序的Java GUI
- 2. 关闭wxpython应用程序挂起gui
- 3. iPad应用程序在2秒后关闭,内存问题?
- 4. 关闭控制台也关闭GUI应用程序
- 5. JAVA GUI关闭应用程序对话框
- 6. 关闭Mac OS X应用程序
- 7. 当应用程序B关闭时关闭应用程序A:Mac OS X 10.7.3
- 8. 如何等待应用程序在OS X中关闭?
- 9. 无法关闭GUI程序
- 10. 关闭在java应用程序中的Javafx类应用程序
- 11. 如何执行GUI Java应用程序?
- 12. Java如何关闭程序并从GUI移动到另一个
- 13. Vaadin 6.x如何关闭应用程序关闭时的外部窗口
- 14. 在关闭程序之前等待x秒钟C++
- 15. 如何在关闭Windows控制台时正常关闭Java应用程序?
- 16. 当点击“X”按钮时,如何关闭WPF应用程序
- 17. 如何在Java中关闭Jpanel程序
- 18. 如何在窗口关闭时关闭JavaFX应用程序?
- 19. Java应用程序在关闭时如何重新启动?
- 20. 如何在Java中处理关闭的应用程序事件?
- 21. Java Webcam GUI应用程序
- 22. Java GUI应用程序
- 23. Java GUI JList应用程序
- 24. Java GUI应用程序?
- 25. 正确关闭Java Web应用程序
- 26. 检测当Java应用程序关闭
- 27. 防止关闭Java swing应用程序
- 28. 完全关闭Java应用程序
- 29. [x]秒后关闭Delphi对话框
- 30. 如何在用户重新启动后10秒内恢复关闭Windows应用商店的应用程序
是否有一个原因以外骚扰用户? – Jeremy
我看了下面的代码:long test = System.currentTimeMillis(); if(test> =(pastTime + 15 * 1000)){//乘以1000得到毫秒 doSomething(); } – RnD
想到javax.swing.Timer(并且在类似情况下对我来说运行良好)。你有什么尝试?编辑:不要使用你的System.currentTimeMillis方法。再次使用[Swing Timer](http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html)。 –