Packagecom.http;Importjava.awt.*;Importjava.awt.event.*;Importjavax.swing.*; Public classSwingjtextcomponentextendsjframe{Private Static Final intTextarea_rows = 8; Private Static Final inttextarer_columns= 20; Publicswingjtextcomponent () {//Create a Text field instance FinalJTextField TextField =NewJTextField (); FinalJPasswordField Passwordfield =NewJPasswordField (); //define a panel, set the table layout Manager and specify rows and columnsJPanel Northpanel =NewJPanel (); Northpanel.setlayout (NewGridLayout (2,2));//add a label for a text fieldNorthpanel.add (NewJLabel ("User Name:", Swingconstants.right));//to add a text field to a panelNorthpanel.add (TextField); Northpanel.add (NewJLabel ("Password:", Swingconstants.right)); Northpanel.add (Passwordfield);//add Pannel to frameAdd (Northpanel,borderlayout.north); //define the text area instance and set the size to add to the frame's center location FinalJTextArea TextArea =NewJTextArea (textarea_rows,textarer_columns); JScrollPane ScrollPane=NewJScrollPane (TextArea); Add (ScrollPane, borderlayout.center); //Define a button, add to the frame below, and define the Listener event, click the button, the text area displays the user name and passwordJPanel Southpanel =NewJPanel (); JButton Insertbutton=NewJButton ("Insert"); Insertbutton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method StubTextarea.append ("User name:" + textfield.gettext () + "Password:" +NewString (Passwordfield.getpassword ()) + "\ n"); } }); Southpanel.add (Insertbutton); Add (Southpanel,borderlayout.south); Pack (); } Public Static voidMain (string[] Argvs) {Eventqueue.invokelater (NewRunnable () { Public voidrun () {JFrame frame=Newswingjtextcomponent (); Frame.settitle ("Swingjtextcomponent"); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setvisible (true); } }); } }
Java-swing-jtextcomponent