1. Single line text input box (jtextfield)
In many cases, you may need to enter some text, and then use the text input box. The single-line text input box is implemented by the jtextfield class. Its constructor is as follows:
(1) jtextfield () constructs a new single line text input box.
(2) jtextfield (INT length) constructs a single line text input box with a specified length.
(3) jtextfield (string text) is used to construct a single line text input box that specifies the initial content.
(4) jtextfield (string text, int length) constructs a single line text input box with a specified length and initial content.
(5) jtextfield (document docu, string text, int length) specifies the file storage mode to construct a single line text input box with the specified length and the specified initial content.
2. Multi-line text input box (jtextarea)
The function of the multi-line text input box is the same as that of the single-line text input box, but it can display more text. Because only one line of text can be entered in a single text input box, multiple lines of text input boxes are required when you need to enter and display more text. Multi-line text input boxes are implemented by the jtextarea class. The jtextarea class has four constructor methods:
(1) jtextarea () constructs a new multi-line text input box.
(2) jtextarea (INT row, int column) constructs a multi-line text input box with a specified length and width.
(3) jtextarea (string text) constructs a multi-line text input box that displays the specified text.
(4) jtextarea (string text, int row, int column) constructs a multi-line text input box that specifies the length, width, and text.
(5) jtextarea (document DOC) constructs a multi-line text input box for the specified file storage mode.
(6) jtextarea (document DOC, string text, int row, int column) constructs a multi-line text input box that specifies the file storage mode, length and width, and displays the specified text.
Iii. Example of using the text input component:
Import Java. AWT. *; import javax. swing. *; public class textdemo extends jframe {public textdemo () {container contentpane = This. getcontentpane (); jpanel jpanel1 = new jpanel (New gridlayout (3, 1); // create a single row text box object, specify the initial value as "one" jtextfield jtextfield1 = new jtextfield ("one"); // create a password box, specify the default initial value as "password", with "{1} quot; display jpasswordfield jpasswordfield1 = new jpasswordfield ("password"); jpasswordfield. setechochar ('
);
// Create a multi-row text box object with the specified initial value in five rows and five columns
Jtextarea jtextarea1 = new jtextarea ("This Is A jtextarea example! ", 5, 5 );
Jpanel1.add (jtextfield1 );
Jpanel1.add (jpasswordfield1 );
Jpanel1.add (jtextarea1 );
Contentpane. Add (jpanel1 );
This. settitle ("textdemo ");
This. setsize (300,200 );
This. setvisible (true );
}
Public static void main (string ARGs []) {
Textdemo test = new textdemo ();
}
}