JDialog is a dialog box component that is often used in conjunction with Joptionpane. Joptionpane provides messages, buttons, and so on inside the dialog box, JDialog provides dialog form, and provides modal/non-modal properties. The difference in appearance between JDialog and JFrame is that it does not have a maximize/minimize button. As shown in the following:
JDialog form
JFrame form
In the following demo, the JDialog constructs a login form and a way to get data from a form.
Logindemostart.java
PackageLogindemo;Importjava.awt.BorderLayout;ImportJava.awt.EventQueue;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjavax.swing.*;/** @ function: Use JDialog to construct login dialog and get login user information * @ Version: 20150804 * @ structure: logindemostart[main class, provide initial ui],passwordchooser[to provide login UI and get user information], user[Save user Information]*/classLogindemostartextendsjframe{/** @ Function: Provide initial UI and call Passwordchooser Login interface * @ Version: 20150804*/ PrivateJTextArea TextArea; PrivatePasswordchooser Passwordchooser; PublicLogindemostart () {//Constructing the menu barJMenuBar mbar =NewJMenuBar (); Setjmenubar (mbar); JMenu Filemenu=NewJMenu ("File"); JMenuItem Connectitem=NewJMenuItem ("Connect"); Connectitem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub//if 1st time,construct a dialogPasswordchooser =NewPasswordchooser (Logindemostart. This); Passwordchooser.setvisible (true); if(Passwordchooser.islogedin ()) {User U=Passwordchooser.getuser (); Textarea.append ("Username =" + u.getname () + ", Password =" +NewString (U.getpassword ()) + "\ n"); } } }); JMenuItem Exititem=NewJMenuItem ("Exit"); Exititem.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method StubSystem.exit (0); } }); Mbar.add (Filemenu); Filemenu.add (Connectitem); Filemenu.add (Exititem); //Add a text areaTextArea =NewJTextArea (); Add (NewJScrollPane (TextArea), borderlayout.center); //Set Form PropertiesSettitle ("Logindemostart"); SetSize (300,200); Setlocationrelativeto (NULL); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubLogindemostart Logindemostart =NewLogindemostart (); Logindemostart.setvisible (true); }}
Passwordchooser.java
PackageLogindemo;Importjava.awt.*;Importjava.awt.event.*;Importjavax.swing.*;/** @ Function: Provide login UI and get user information * @ Version: 20150804*/classPasswordchooserextendsjdialog{PrivateJTextField username; PrivateJPasswordField password; PrivateJButton OKButton; PrivateJButton CancelButton; Private BooleanIslogedin =false; PublicPasswordchooser (Frame parent) {Super(Parent,true); //This UI consists of 2 panelJPanel InputPanel =NewJPanel (); JPanel Buttonpanel=NewJPanel (); //Construction InputPanelInputpanel.setlayout (NewGridLayout (2,2)); Inputpanel.add (NewJLabel ("Useername:")); Username=NewJTextField (); Username.setcolumns (10); Password=NewJPasswordField (); Password.setcolumns (10); Inputpanel.add (username); Inputpanel.add (NewJLabel ("Password:")); Inputpanel.add (password); Inputpanel.setborder (Borderfactory.createemptyborder (10, 5, 10, 5)); //Construction ButtonpanelOKButton =NewJButton ("OK"); Okbutton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method StubIslogedin =true; SetVisible (false); } }); CancelButton=NewJButton ("Cancel"); Cancelbutton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method StubSetVisible (false); } }); Buttonpanel.add (OKButton); Buttonpanel.add (CancelButton); Buttonpanel.setborder (Borderfactory.createemptyborder (10, 5, 10, 5)); //construct the main frameSetLayout (NewBorderLayout ()); Getcontentpane (). Add (InputPanel, Borderlayout.north); Getcontentpane (). Add (Buttonpanel, Borderlayout.south); //Set Form PropertiesSettitle ("Passwordchooser"); Setlocationrelativeto (InputPanel); //setpreferredsize (new Dimension (+));pack (); //System.out.println (getPreferredSize ()); } Public voidSetUser (User u) {username.settext (U.getname ()); } PublicUser GetUser () {return NewUser (Username.gettext (), Password.getpassword ()); } Public BooleanIslogedin () {returnIslogedin; } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub }}
User.java
PackageLogindemo;/** @ Function: Save user Information * @ Version: 20150804*/classuser{PrivateString name; Private Char[] password; PublicUser (String AName,Char[] apassword) {Name=AName; Password=Apassword; } PublicString GetName () {returnname; } Public Char[] GetPassword () {returnpassword; } Public voidsetName (String aName) {name=AName; } Public voidSetPassword (Char[] apassword) {Password=Apassword; }}
Swing-jdialog Sample code-User Login UI