This article introduces the specific implementation of the swing standard dialog box. If you need it, refer to it.Copy codeThe Code is 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. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Jf. setVisible (true );
}
@ Override
Public void actionreceivmed (ActionEvent e ){
// TODO Auto-generated method stub
String s = e. getActionCommand ();
If (s. equals ("error ")){
JOptionPane. showMessageDialog (null, "error message to be displayed ---",
"Error message", JOptionPane. ERROR_MESSAGE );
}
Else if (s. equals ("Exit Confirmation 1 ")){
Int result = JOptionPane. showConfirmDialog (null,
"Do you want to save the program before release? ");
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 cause data loss", "Warning", JOptionPane. DEFAULT_OPTION,
JOptionPane. WARNING_MESSAGE, null, options, options [0]);
If (result = 0 ){
System. out. println ("continue operation ---");
}
}
Else if (s. equals ("input ")){
String name = JOptionPane. showInputDialog ("enter your name :");
If (name! = Null ){
System. out. println ("name:" + name );
}
}
Else if (s. equals ("select ")){
Object [] possibleValues = {"Sports", "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 selected:" + choose );
}
}
}
}