Joptionpane dialog box

Source: Internet
Author: User

The static factory method of the Joptionpane class primarily creates 4 types of dialog boxes

&1 showmessagedialog----Display a modal dialog box with OK button (only the currently popup dialog is executed)

JAVA instance:

Joptionpane.showmessagedialog (JL, "Welcome to our Store", "Dialog's title", Joptionpane.information_message);


&2 Showoptiondialog-----This function can change the text displayed on the button, and you can customize the options

Java instance:

Object [] options = {"Lily", "White Rose", "Little Daisy", "Yellow Rose"};

int index = Joptionpane.showoptiondialog (JP, "What flowers do you like?") "," dialog box title ", Joptionpane.yes_no_cancel_option,joptionpane.question_message,null,options,options[3]);


&3 Showconfirmdialog----This function uses only two options yes/no

Java instance:

int n = joptionpane.showconfirmdialog (JL, "Did you have a good last?") "," dialog box title ", Joptionpane.yes_no_option,joptionpane.question_message);


&4 Showinputdialog-----This function is used to add content directly to a text box

Joptionpane.showinputdialog (JL, "Please enter the dish you most want to eat:", "dialog title", Joptionpane.plain_message);


Attention:

The question_message and so on here are used to establish an icon from the pluggable appearance, usage: joptiondialog.question_message

Constant name Purpose
Error_message for error messages
Information_message for informational messages
Warning_message for warning messages
Plain_message for any information
Question_message for problem information

In particular, the constants in the Joptiondialog class that represent option types are as follows, usage: joptiondialog.yes_no_option

Constant name Purpose
Default_option does not provide any options
Yes_no_option yes,no Options
Yes_no_cancel_option yes,no,cancel Options
Yes_cancel_option yes,cancel Options
Small Sample order:

Package Ch10;import Java.awt.borderlayout;import java.awt.gridlayout;import java.awt.event.*;import javax.swing.*;    public class Dialogtest extends JFrame implements actionlistener{JPanel JP = new JPanel ();    JButton jb1 = new JButton ("Only OK button");    JButton jb2 = new JButton ("yes/no button");    JButton jb3 = new JButton ("Yes/no/cancle3 button");    JButton jb4 = new JButton ("Yse/no/cancle3 button (custom)");    JButton jb5 = new JButton ("Input message dialog box");    JButton jb6 = new JButton ("Options dialog box");    JButton []jbuttonarray = new Jbutton[]{jb1,jb2,jb3,jb4,jb5,jb6}; JLabel JL = new JLabel ("Click the button and you will get a different dialog box!")    ");    Public Dialogtest () {jp.setlayout (new GridLayout (2,3));    for (int i=0;i<jbuttonarray.length;i++) {jp.add (jbuttonarray[i]);    Jbuttonarray[i].addactionlistener (this);    } this.add (JP);    This.add (Jl,borderlayout.south);    This.settitle ("Joptionpane dialog box");    This.setbounds (200,200,500,200);    This.setvisible (TRUE);    This.setdefaultcloseoperation (Jframe.exit_on_close); }    public void actionperformed (ActionEvent a) {if (A.getsource () ==jbuttonarray[0]) {joptionpane.showmessaged Ialog (This, "Welcome to our store!")    "," Only the OK button of the message dialog box ", Joptionpane.information_message);    Jl.settext ("Welcome, welcome, warm welcome"); } else if (A.getsource () ==jbuttonarray[1]) {int index = Joptionpane.showconfirmdialog (this, "Hello, is this your first time to visit our store?").    "," There is a confirmation dialog for the yes/on button ", joptionpane.yes_no_option,joptionpane.question_message); Jl.settext ("You Are" + ((index==0)? ") New customers, welcome ":" Old customers, welcome.    "));    } else if (A.getsource () ==jbuttonarray[2]) {object[] options = {"Likes", "does not like"}; int index = Joptionpane.showoptiondialog (this, "Hello, do you like to eat pickled cabbage fish?")    "," with yes/no button (custom) confirmation dialog box ", Joptionpane.yes_no_option,joptionpane.question_message,null,options,options[0]); Jl.settext ("I wrote down, you" + (index==0)? "    Like ":" Do not like ") +" eat sauerkraut fish ");    } else if (A.getsource () ==jbuttonarray[3]) {object[] options = {"Well, give me a copy", "No, we have enough food", "give me a spicy shrimp"}; int index = Joptionpane.showoptiondialog (this, "Hello, Taste the restaurant's signature cabbage fish?" "," with yes/no/cancel_option Confirmation dialog ", JoptiOnpane.yes_no_cancel_option,joptionpane.question_message,null,options, options[2]); String reply = (index = = 0)? " Wait a minute, the pickled cabbage fish you ordered soon arrived ":(index==1)?" OK, need to call me again ":(index==2)? "OK, you ordered the spicy shrimp soon to": "Sorry, please order!    ";    Jl.settext (reply); } else if (A.getsource () ==jbuttonarray[4]) {String dishes = Joptionpane.showinputdialog (this, "Please enter your most wanted dish:", "Enter the information dialog    Box ", joptionpane.plain_message);    Jl.settext (dishes);    } else if (A.getsource () ==jbuttonarray[5]) {string[] options = new String [] {"Spicy shrimp", "pickled cabbage fish", "Public Security beef Fish Miscellaneous", "Crock Soup"}; int index = Joptionpane.showoptiondialog (This, "below is our free dish, please choose one, the default is Crock soup", "Options Dialog", Joptionpane.default_option,    JOPTIONPANE.PLAIN_MESSAGE,NULL,OPTIONS,OPTIONS[3]); String dishes = (index==0)? " Spicy Shrimp ":(index==1)?" Pickled cabbage Fish ":(index==2)?"    Public Security Beef Fish Miscellaneous ":" Crock Soup ";    Jl.settext ("You have chosen the gift of the +dishes+"!);}}    public static void Main (String args[]) {new dialogtest (); }}



Joptionpane dialog box

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.