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);
}
}
}
}