In the graphic interface, the text box and text area are components used for information input and output.
Text box
The text box (jtextField) is a box used to input and output a line of text in the interface. The JTextField class is used to create a text box. The interface related to the text box is ActionListener.
The basic content of the text box processing program has the following aspects:
1. Declarize a text box name.
2. Create a text box object.
3. Add the text box object to a certain container.
4. Register the monitor for the text box object that needs to be controlled, and the input of the text box ends (ie input the Enter key) event.
5. A method of processing the text box event to complete the judgment and processing of interception.
The main constructing method of the JTextField class:
1. JTextField (), the character length of the text box is 1.
2.jtextField (int columns), the initial value of the text box is an empty string, and the character length of the text box is set to colorns.
3. JTextField (String Text), the initial value of the text box is a text string.
4. JTextField (String Text, Int Columns); the initial value of the text box is Text, and the character length of the text box is Columns.
Other methods of the JTextField class:
1.Setfont (font f), set font
2.Settext (String Text), set text in the text box
3.sttext (), get text in the text box.
4.SetedITable (Boolean), specifying the editability of the text box, the default is true, editable.
5.Sethorizontalallyignment (int Alignment) Setting text alignment method. Alignment methods are: jtextField.left, jtextField.center, jtextField.richt.
6.requestfocus (), set the focus.
7.AdDactionListener (ActionListener), set an action monitor for the text box, and specify the ActionListener object to receive the input ending event incident on the text box.
8.RmoveActionListener (ActionListener) Move the text box monitor.
9.Getcolumns (), return the number of columns in the text box.
10.Getminimumsize (), the minimum size required to return to the text box.
11.Getminimumsize (int), returns the minimum size required for the number of characters in the specified character.
12.Getpreferredsize (), returns the size of the text box hopes.
13.GetPreferredsize (int), return the text box to the size of the specified character, hopes to have the size.
[Example 11-8] Small applications have two text boxes. One text is used to enter one integer, and the other text box displays the square value of this integer. The program uses a string to rotate the basic type of type of basic types of LONG.PARSELONG (text1.gettext ()), read the string in text box Text1, and convert it into an integer. The program uses an instance of the SQR class as a monitor, but in order to allow the monitor to access the variables of the main class, the variables in the main class are declared as a class variable and no access permissions are set.
Import java.applet.*; Import javax.swing.*; Import Java.awt.event.*; Public Class J508 EXTENDS Applet {Static JTextField Text1, Text2; SQR S = new sqr (); // Create a monitor Public Void init () {text1 = new jtextField (10); text2 = new jtextField (10); add (text1); add (text2); text1.adDactionListener (s); } } Class SQR Implements ActionListener {Public Void ActionPerformed (ActionEvent E) {// Implement interface ActionListener if (e.getsource () == J508.Text1) Parselong (j508.text1.gettext ()); / /Convert the text of Text1 to the LONG type data J508.Text2.Settext (String.Valueof (n*N)); // Convert n*n to string} Else {}}}} The password box (JPASSWORDFIELD) is a one -way input component, which is basically similar to JTEXTFIELD. One more shielding function of the password box is that when input, it will be output with one specified character (usually*character). In addition to the method of text boxes introduced earlier, there are some commonly used methods for password boxes:
1.Getechochar (), return the back -to -display character of the password.
2.Setechochar (Char), set the back -display character of the password box.
Text area
The text area (JTEXTARA) is a area where text is placed in the window. The main difference between the text area and the text box is that the text area can be stored in multiple lines of text. The JTExtarea class in the Javax.swing bag is used to create a text area. JTextarea components have no incident.
The basic content of the text area processing program has the following aspects:
1. Declarize a text area name.
2. Create a text zone object.
3. Add the text zone object to a container.
The main structural method of the JTextarea class:
1. JTextarea (), create a text zone object with the default number and row.
2. JTextarea (String S), with S as the initial value, create a text zone object.
3. JTextarea (Strings, int x, int y), with s as the initial value, the number of rows is x, and the number of columns is y, creating a text zone object.
4. JTextarea (int x, int y) uses the number of rows to X and the number of columns as y to create a text zone object.
Other common methods of the JTextarea class:
1.Settext (string s), set the display text, and remove the original text at the same time.
2.gettext (), get text in the text area.
3.Insert (string s, int x), insert the specified text at the specified location.
4.Ruplace (String s, int x, int y), use a given text to end the text from the X position to the y position.
5. !Ppend (string s), added text in the text area.
6.GetCarepositation (), get the position of the activity cursor in the text area.
7.setCarepositation (int N), set the position of the activity cursor.
8.SetLineWrap (Boolean B), set automatic changes, default situations, and do not switch.
The following code creates a text area and sets up automatically.
Jtextarea texta = New JTextarea ("I am a text area", 10,15);
TextA.SetLinewrap (True); // Set automatic change as the content in the text area. When it is not displayed in the text area, you can match the rolling bar for the text area. The following code can be used to set up rolling bars to the text area:
Jtextarea Ta = new jtextarea (); jscrollpane jsp = new jscrollpane (ta); // Add rolling bars to the text area
The above is all the contents of this article. I hope everyone can like it.