我需要一些关于我使用NetBeans开发的Java GUI的帮助。我想要的功能,用户点击一个按钮退出应用程序。而不是立即退出,我希望弹出一条消息说“您的作业已提交,此窗口将在10秒内关闭”。也许会显示从10到0的倒数。
发布于 2011-08-25 03:25:27
代码如下:(感谢http://www.coderanch.com/t/341814/GUI/java/JOptionPane-Timeout的Ko Wey )
# //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 发布于 2011-08-24 10:06:17
JOptionTest就是一个使用javax.swing.Timer的例子。
发布于 2011-08-24 09:30:42
有关如何实现Java对话的信息,请查看教程How to Make Dialogs。如果你想在关闭应用程序之前执行一些任务,你可以在你的对话窗口中显示progress。
https://stackoverflow.com/questions/7169463
复制相似问题