Swing-jdialog Sample code-User Login UI

Source: Internet
Author: User

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

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.