Java know how much (85) text box and text area

Source: Internet
Author: User
Tags gettext

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

text box

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

The basic content of a text box handler is as follows:

    1. Declares a text box name.
    2. Creates a text box object.
    3. Joins a text box object to a container.
    4. Register the monitor with the text box object that you want to control, and listen for the input end of the text box (that is, enter a enter) event.
    5. A method of handling the text box event to complete the judgment and processing of the intercepted event.


The main construction methods of the JTextField class are:

    1. JTextField (), the text box has a character length of 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), the text box initial value is the string of text.
    4. JTextField (String text,int columns), text box initial value is text, text box character length is columns.


Other methods of the JTextField class:

    1. SetFont (font f), set font
    2. SetText (String text), setting the text in the text box
    3. GetText () to get the text in the text box.
    4. Seteditable (Boolean), which specifies the editable nature of the text box, which is true by default and editable.
    5. sethorizontalalignment (int alignment) sets the text alignment. The alignment is: Jtextfield.left, Jtextfield.center, Jtextfield.right.
    6. Requestfocus (), set focus.
    7. addActionListener (ActionListener), sets the action monitor for the text box, specifying that the ActionListener object receives an input end action event that occurs on the text box.
    8. Removeactionlistener (ActionListener) Removes the text box monitor.
    9. GetColumns () returns the number of columns in the text box.
    10. Getminimumsize (), returns the minimum size required for the text box.
    11. getminimumsize (int), which returns the minimum size required for a text box in the specified number of characters.
    12. getPreferredSize () returns the size that the text box expects to have.
    13. getPreferredSize (int) that returns the size of the text box that you want to have in the specified number of characters.


The "Example 11-8" 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 a string-to-primitive type method Long.parselong (Text1.gettext ()), reads the string in the text box 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 permissions are not set.

1 Importjava.applet.*;Importjavax.swing.*;Importjava.awt.event.*;2  Public classJ508extendsapplet{3     StaticJTextField text1,text2;4SQR s=NewSQR ();//Create a Monitor5      Public voidinit () {6text1=NewJTextField (10);7Text2=NewJTextField (10);8 Add (Text1);9 Add (text2);TenText1.addactionlistener (s);//instance of class Sqr S as a monitor for Text1 One     } A } - classSqrImplementsactionlistener{ -      Public voidactionperformed (ActionEvent e) {//Implement Interface ActionListener the         if(E.getsource () = =j508.text1) { -             Longn=Long.parselong (J508.text1.getText ()); -             //Convert Text1 text to Long data -J508.text2.setText (string.valueof (nn)); +             //Convert n*n to a string -         } +         Else{} A     } at}

The Password box (JPasswordField) is a single-line input component that is basically similar to JTextField. Password box more than one shielding function, that is, when the input, will be a different specified character (usually * character) output. In addition to the text box methods described earlier, there are some common methods of password boxes:

    1. 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 in 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 a text area. The JTextArea component has no events.

The basic content of a text area handler is as follows:

    1. Declares a text area name.
    2. Creates a text area object.
    3. Joins a text area object to a container.


The main construction methods of the JTextArea class are:

    1. JTextArea (), creates a text-area object with the default number of columns and the number of rows.
    2. JTextArea (String s), which is the initial value of S, creates a text area object.
    3. JTextArea (Strings, int x,int y), the initial value of S, the number of rows is x, the number of columns is Y, and a text area object is created.
    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), sets the display text, and clears the original text.
    2. GetText () to get the text of the text area.
    3. Insert (String S,int x) that inserts the specified text at the specified position.
    4. Replace (String s,int x,int y), replacing the text with the given one from the X position to the end of the Y position.
    5. Append (String s), appending text to the text area.
    6. Getcareposition () to get the position of the active cursor in the text area.
    7. setcareposition (int n), sets the position of the active cursor.
    8. Setlinewrap (Boolean B), sets the line wrapping, by default, does not wrap.


The following code creates a text area and sets the lines to wrap.
JTextArea texta = new JTextArea ("I am a text area", 10,15);
Texta.setlinewrap (TRUE);//Set line wrapping
When there is more content in the text area and you cannot display it all in the text area, you can put a scroll bar on the text area. The following code is available to set the scrollbar for the text area:

1     New JTextArea (); 2     New JScrollPane (TA); // Add scroll bars to the text area

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interface

Java know how much (85) text box and text area

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.