Java Swing Standard dialog box implementation

Source: Internet
Author: User
Tags exit int size stub
This article describes the implementation of the Swing Standard dialog box, you need friends can refer to the copy code code as follows:




package test001;


import java.awt.event.ActionEvent;


import Java.awt.event.ActionListener;


import Javax.swing.JButton;


import Javax.swing.JFrame;


import Javax.swing.JOptionPane;


import Javax.swing.JToolBar;


public class Testjoptionpane implements actionlistener{


private JFrame JF = new JFrame ("Standard dialog Box test");


/**


* @param args


*/


public static void Main (string[] args) {


//TODO auto-generated method stub


new Testjoptionpane (). Createui ();


}


public void Createui () {


JToolBar JTB = new JToolBar ();


string[] s = {"Error", "Exit Confirmation 1", "Exit Confirmation 2", "Warning", "Input", "select"};


int size = s.length;


jbutton[] button = new Jbutton[size];


for (int i = 0; i < size; i++) {


Button[i] = new JButton (s[i));


Button[i].addactionlistener (this);


Jtb.add (Button[i]);


}


Jf.add (JTB, "North");


jf.setsize (350, 150);


jf.setlocation (400, 200);


jf.setdefaultcloseoperation (jframe.exit_on_close);


jf.setvisible (TRUE);


}


@Override


public void actionperformed (ActionEvent e) {


//TODO auto-generated method stub


String s = E.getactioncommand ();


if (s.equals ("error")) {


Joptionpane.showmessagedialog (NULL, "error message to display---",


"false hint", joptionpane.error_message);


}


Else if (s.equals ("Exit confirmation 1")) {


int result = Joptionpane.showconfirmdialog (null,


"Do you want to save the program before launch?" ");


if (result = = joptionpane.yes_option) {


System.out.println ("Save Program---");


system.exit (0);


}


else if (result = = joptionpane.no_option) {


system.exit (0);


}


}


Else if (s.equals ("Exit confirmation 2")) {


int result = Joptionpane.showconfirmdialog (null, "Do you want to save the program before exiting?) ");


if (result = = joptionpane.yes_option) {


System.out.println ("Save Program---");


system.exit (0);


}


else if (result = = joptionpane.no_option) {


system.exit (0);


}


}


Else if (s.equals ("Warning")) {


object[] options = {"Continue", "undo"};


int result = Joptionpane.showoptiondialog (null,


"This operation may result in data loss", "Warning", Joptionpane.default_option,


joptionpane.warning_message, NULL, Options, options[0]);


if (result = = 0) {


System.out.println ("continue to operate---");


}


}


Else if (s.equals ("input")) {


String name = Joptionpane.showinputdialog ("Please enter your name:");


if (name!= null) {


System.out.println ("Name:" + name);


}


}


Else if (s.equals ("select")) {


object[] possiblevalues = {"Sport", "politics", "economy", "culture"};


Object selectedvalue = Joptionpane.showinputdialog (null,


"Choose One", "Input", joptionpane.information_message, NULL,


PossibleValues, possiblevalues[0]);


String choose = (string) SelectedValue;


if (choose!= null) {


System.out.println ("you choose:" + choose);


}


}


}


}

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.