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