A brief discussion of the Java Chinese text box and text section

  • 2020-04-01 03:54:53
  • OfStack

In a graphical interface, text boxes and text areas are the components used for input and output of information.

The text box

A text box (JTextField) is a box in the interface for input and output of a line of text. The JTextField class is used to create text boxes. The interface associated with the text box is the ActionListener.

The basic content of a text box handler has the following aspects:
Declare a textbox name.
2. Create a text box object.
3. Adds a text box object to a container.
4. Register the monitor for the textbox object to be controlled, and listen for the input end (that is, the enter key) event of the textbox.
5. A method to handle text box events, complete the judgment and processing of intercepted events.

The main constructor of the JTextField class:
1.JTextField(), the character length of the text field 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 columns.
3.JTextField(String text), a String of text fields with the initial value of text.
4. A 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 the Font
2. SetText (String text), which sets the text in the text box
GetText (), get the text in the text box.
4. SetEditable (Boolean), which specifies the editable value of the text box, true by default, editable.
5. SetHorizontalAlignment (int alignment) sets the alignment of the text. Alignment: jtextfield.left, jtextfield.center, jtextfield.right.
RequestFocus (), which sets the focus.
7. AddActionListener (ActionListener), which sets the action monitor for the text box, and specifies that the ActionListener object receives the input end action event that occurs on the text box.
8. RemoveActionListener (ActionListener) removes the textbox monitor.
9. GetColumns (), returns the number of columns in the text box.
GetMinimumSize (), returns the minimum size required for the text box.
GetMinimumSize (int), returns the minimum size required for the text box with the specified number of characters.
12. GetPreferredSize (), which returns the size the text box wants to have.
GetPreferredSize (int), which returns the desired size of the text box for the specified number of characters.

The applet has two text boxes. One text is used to enter an integer, and the other text box displays the square value of the integer. The program USES the string to transform the primitive type method long.parselong (text1.gettext ()), reads the string in text1 and converts it to an integer. The program USES an instance of the Sqr class as the monitor, but in order for the monitor to access the variables of the main class, the variables in the main class are declared as class variables and access is not 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 monitor
   public void init(){
     text1=new JTextField(10);
     text2=new JTextField(10);
     add(text1);
     add(text2);
     text1.addActionListener(s);//The instance s of class Sqr ACTS as the monitor for text1
   }
 }
 class Sqr implements ActionListener{
   public void actionPerformed(ActionEvent e){//Implement the interface ActionListener
     if(e.getSource()==J508.text1){
       long n=Long.parseLong(J508.text1.getText());
       //Converts the text of text1 to long data
       J508.text2.setText(String.valueOf(n*n));
       //Convert n by n to a string
     }
     else{}
   }
 }

A password box (JPasswordField) is a single-line input component, much like a JTextField. Password box more than a masking function, is in the input, will be a specified character (generally * character) output. In addition to the text box methods described above, there are some other methods commonly used for password boxes:
GetEchoChar (), returns the echo character of the password.
2. SetEchoChar (char), sets the echo character of the password box.

Text area

A text area (JTextArea) is an area on a form where text is placed. The main difference between a text area and a text box is that the text area can hold multiple lines of text. The JTextArea class in the javax.swing package is used to create text areas. The JTextArea component has no events.

The basic content of a text area handler has the following aspects:
Declare a text field name.
2. Create a text area object.
Add a text area object to a container.

The main construction method of JTextArea class:
1.JTextArea(), creates a text area object with the default number of columns and rows.
2.JTextArea(String s), with s as the initial value, creates a text area object.
3.JTextArea(Strings,int x,int y), with s as the initial value, the number of rows is x, the number of columns is y, create a text area object.
4.JTextArea(int x,int y) creates a text area object with the number of rows as x and the number of columns as y.

Other common methods of the JTextArea class:
1. SetText (String s), set to display text while clearing the original text.
GetText (), get the text in the text area.
3. Insert (String s,int x) to insert the specified text at the specified location.
4. Replace (String s,int x,int y), replace the text from the x position to the y position with the given one.
5. Append (String s), append text to the text area.
GetCarePosition (), gets the position of the active cursor in the text area.
7. SetCarePosition (int n), which sets the position of the active cursor.
8. SetLineWrap (Boolean b), set to wrap, by default, no wrap.

The following code creates a text area and sets it to wrap automatically.
      JTextArea textA = new JTextArea(" I am a text area ",10,15);
      TextA. SetLineWrap (true); // set line wrap
When there is too much content in the text area to display all the text area, the text area can be equipped with a scroll bar. The following code can be used to set the scroll bar for the text area:


JTextArea ta = new JTextArea();
JScrollPane jsp = new JScrollPane(ta);//Add a scroll bar to the text area

The above is all the content of this article, I hope you can enjoy it.


Related articles: