Java Custom Confirm Canceled Dialog dialog box, auto shutdown after 10 seconds

Source: Internet
Author: User

The questions are as follows:

Click on a button to pop up a dialog box containing the OK and Cancel buttons

If click OK, return value is 0, click Cancel, return value is 1

If not clicked for more than 10 seconds, the prompt window closes automatically, and the return value is-5

The following can be used to make a variety of judgments based on the return value.

1.TimerTest Main program, run the Main method first, then click the button

Timertest.java

Package com.chat.client;
Import java.awt.Dimension;
Import java.awt.event.ActionEvent;

Import Java.awt.event.ActionListener;
Import Javax.swing.JButton;

Import Javax.swing.JFrame;

Import Com.chat.ui.TimeDialog;
    public class Timertest extends JFrame {/** * */private static final long serialversionuid = 1L;
    private static JButton button;

	private static Timertest timertest;
		public static void Main (string[] args) {timertest = new timertest ();
		button = new JButton ("Press Me"); Button.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {Tim
			   Edialog d = new Timedialog (); int result = D.showdialog (Timertest, "does the other person want to have your voice accepted?");//Timertest is the main window class, pop-up dialog box disappears after 10 seconds System.out.println ("===res
			Ult: "+result);
		
		}
		});
		Button.setbounds (2, 5, 80,20);
		Timertest.getcontentpane (). setlayout (NULL);
		Timertest.getcontentpane (). Add (button);
		Timertest.setsize (New Dimension (400,200)); Timertest.setlocation(500,200);
		Timertest.setvisible (TRUE);

	Timertest.setdefaultcloseoperation (Jframe.exit_on_close);
 }

}
2. Custom Dialog

Timedialog.java

Package com.chat.ui;
Import java.awt.Dimension;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;

Import Java.util.concurrent.TimeUnit;
Import Javax.swing.JButton;
Import Javax.swing.JDialog;
Import Javax.swing.JFrame;

Import Javax.swing.JLabel;
    public class Timedialog {private String message = NULL;
    private int secends = 0;
    Private JLabel label = new JLabel (); 
    Private JButton Confirm,cancel;
    Private JDialog dialog = null;
    int result =-5;
        public int ShowDialog (JFrame father, String message, int sec) {this.message = message;
        Secends = sec;
        Label.settext (message);
        Label.setbounds (80,6,200,20);
        Scheduledexecutorservice s = executors.newsinglethreadscheduledexecutor ();
        Confirm = new JButton ("accept");
        Confirm.setbounds (100,40,60,20); Confirm.addactionlistener (New ActionListener ({@Override public void actionperformed (ActionEvent e) {result = 0;
			TimeDialog.this.dialog.dispose ();
        }
		});
        Cancel = new JButton ("reject");
        Cancel.setbounds (190,40,60,20); Cancel.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {Resul
				t = 1;
			TimeDialog.this.dialog.dispose ();
        }
		});
        dialog = new JDialog (father, true);
        Dialog.settitle ("Hint: This window will be closed automatically after" +secends+ ");
        Dialog.setlayout (NULL);
        Dialog.add (label);
        Dialog.add (confirm);
        Dialog.add (cancel);
                S.scheduleatfixedrate (New Runnable () {@Override public void run () {
                TODO auto-generated method Stub timedialog.this.secends--;
                if (TimeDialog.this.secends = = 0) {TimeDialog.this.dialog.dispose (); }else {dialog.settitle ("hint: This window will be automatically closed after "+secends+" seconds);
        }}, 1, 1, timeunit.seconds);
        Dialog.pack ();
        Dialog.setsize (New Dimension (350,100));
        Dialog.setlocationrelativeto (father);
        Dialog.setvisible (TRUE);
    return result;
 }
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.