A brief talk on Java Chinese box and text area _java

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 that you use to enter and output one line of text. The JTextField class is used to create text boxes. The interface associated with the text box is ActionListener.

The basic contents of the text box handler are as follows:
1. Declare a text box name.
2. Create a text box object.
3. Add a text box object to a container.
4. For the text box object registration Monitor that needs to be controlled, the input end of the Listening text box (that is, enter the ENTER key) event.
5. A method of handling text box events to complete the judgement and processing of interception events.

The main construction method of the JTextField class:
1.JTextField (), the character length of the text box is 1.
2.JTextField (int columns), the text box initial value is an empty string, the text box character length is set to columns.
3.JTextField (string text), the text box has the initial value of a string.
4.JTextField (String text,int columns), text box initial value is text, text box character length is columns.

Other ways to JTextField classes:
1.setFont (font f), setting the font
2.setText (String text), setting text in a text box
3.getText () to get the text in the text box.
4.setEditable (Boolean), which specifies the editable text box, the default is true, and editable.
5.setHorizontalAlignment (int alignment) sets the text alignment. Alignment is: Jtextfield.left, Jtextfield.center, Jtextfield.right.
6.requestFocus () to set the focus.
7.addActionListener (ActionListener), sets the action monitor for the text box, specifying that the ActionListener object receives input end action events that occur on the text box.
8.removeActionListener (ActionListener) To remove 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), returns the minimum size required for the text box in the specified number of characters.
12.getPreferredSize () returns the size that the text box would like to have.
13.getPreferredSize (int), returns the size of the text box that you want to have in the specified number of characters.

The example 11-8 small application has two text boxes. One text is used to enter an integer and another text box displays the square value of the integer. The program Long.parselong (Text1.gettext ()) The method of the base type with a string, reads the string in the text box Text1, and converts it to an integer. The program uses an instance of the Sqr class as a monitor, but in order for the monitor to have access to the variables of the main class, the variables in the main class are declared as class variables and do not set access permissions.

 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 ();
     Text2=new JTextField (a);
     Add (Text1);
     Add (text2);
     Text1.addactionlistener (s);//class SQR instance s as monitor of Text1
 class SQR implements
   actionlistener{ public void actionperformed (ActionEvent e) {//implementation interface ActionListener
     if (E.getsource () ==j508.text1) {
       long n= Long.parselong (J508.text1.getText ());
       Converts the Text1 text to a Long data
       J508.text2.setText (string.valueof (n*n));
       Convert N*n to String

The Password box (JPasswordField) is a single line of input components, similar to JTextField. The Password box One more shielding function, is in the input, will be one other specified character (generally is * character) output. In addition to the methods of the text boxes described earlier, there are some common methods for password boxes:
1.getEchoChar (), returns the Echo character of the password.
2.setEchoChar (char), set 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 holds multiple lines of text. The JTextArea class in the Javax.swing package is used to create the text area. The JTextArea component has no events.

The basic content of a text area handler is as follows:
1. Declare a text area name.
2. Create a text area object.
3. Add a text area object to a container.

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

Other common ways to JTextArea classes:
1.setText (String s), sets the display text and clears the original text.
2.getText (), gets the text of the text area.
3.insert (String s,int x) inserts the specified text at the specified location.
4.replace (String s,int x,int y), with the given one replaces the text from the X position to 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), set the position of the active cursor.
8.setLineWrap (Boolean B), set to wrap automatically, by default, does not wrap automatically.

The following code creates a text area and sets the line to wrap automatically.
JTextArea texta = new JTextArea ("I am a text area", 10,15);
Texta.setlinewrap (TRUE);//Set Wrap line
When there is more content in the text area and cannot be displayed in the text area, scroll bars can be used to fit the text area. The following code is available for setting scroll bars for the text area:

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

The above mentioned is the entire content of this article, I hope you can enjoy.

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.