Java image interface development simple example-simple application of jtextfield, jpasswordfield, jbutton and events

Source: Internet
Author: User

Java image interface development example

A simple application of jtextfield, jpasswordfield, jbutton, and events is used to create a logon interface to display the entered user name and password. The Code is as follows:

Import java. AWT. dimension;
Import java. AWT. gridlayout;
Import java. AWT. toolkit;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;

Import javax. Swing. jbutton;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. joptionpane;
Import javax. Swing. jpasswordfield;
Import javax. Swing. jtextfield;

/**
* Simple application example of jtextfield, jpasswordfield, jbutton, and events
* @ Author Zuo Jie
*
*/

Public class example6frame extends jframe {

/**
*
*/
Private Static final long serialversionuid = 1l;
Private jtextfield username; // user name input box
Private jpasswordfield password; // password input box
Private jbutton logonbutton; // The logon button.
Private jbutton cancelbutton; // exit button

Public example6frame (){
Settitle ("Logon Demo"); // set the title of the form
Setsize (300,120); // set the size of the form
Setlayout (New gridlayout (3, 2, 2, 2); // set the layout manager (four parameters, table sharding indicates the number of rows, columns, horizontal spacing, and Vertical spacing)
Setresizable (false); // The window size cannot be changed.
// Set the display in the middle of the screen
Dimension screensize = toolkit. getdefatooltoolkit (). getscreensize ();
Dimension framesize = This. getsize ();
If (framesize. Height> screensize. Height ){
Framesize. Height = screensize. height;
}
If (framesize. width> screensize. width ){
Framesize. width = screensize. width;
}
This. setlocation (screensize. Width-framesize. width)/2,
(Screensize. Height-framesize. Height)/2 );
Username = new jtextfield (16); // initialize the text input box, with a width of 16 Columns
Password = new jpasswordfield (16); // The initialization password input box, with a width of 16 Columns
Logonbutton = new jbutton ("login"); // initialize the logon button
Logonbutton. addactionlistener (// handle logon button events
New actionlistener (){
Public void actionreceivmed (actionevent EVT ){
Char [] PW = password. GetPassword (); // get the password
String message = "your Username:" + username. gettext ()
+ "Your password:" + new string (PW); // message string
Joptionpane. showmessagedialog (null, message); // display the message
}
});
Cancelbutton = new jbutton ("exit"); // initialize the exit button
Cancelbutton. addactionlistener (// initialization button event processing
New actionlistener (){
Public void actionreceivmed (actionevent EVT ){
System. Exit (0); // exit the program
}
});
// Add the corresponding component to the form
Add (New jlabel ("User name :"));
Add (username );
Add (New jlabel ("Password :"));
Add (password );
Add (logonbutton );
Add (cancelbutton );
}

Public static void main (string [] ARGs ){
Jframe frame = new example6frame ();
Frame. setdefaclocloseoperation (jframe. exit_on_close); // exit the program when the window is closed.
Frame. setvisible (true); // display the form
}

}

 

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.