Java swing standard dialog box implementation

Source: Internet
Author: User

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

 

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.