Parsing text boxes and text areas in Java graphical programming _java

Source: Internet
Author: User
Tags gettext numeric value

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:

    • Declares a text box name.
    • Creates a text box object.
    • Adds a text box object to a container.
    • For the text box object registration monitor that you want to control, the input of the Listening text box ends (that is, enter the ENTER key) event.
    • A method of handling text box events, which is to judge and deal with the interception events.

The main construction method of the JTextField class:

    • JTextField (), the character length of the text box is 1.
    • JTextField (int columns), the text box initial value is an empty string, the text box character length is set to columns.
    • JTextField (string text), the text box with the initial value of a string.
    • JTextField (String text,int columns); The text box initial value is text and the text box has a character length of columns.

Other ways to JTextField classes:

    • SetFont (font f), setting fonts
    • SetText (String text), setting text in a text box
    • GetText () to get the text in the text box.
    • Seteditable (Boolean) that specifies the editable text box, the default is true, and editable.
    • sethorizontalalignment (int alignment) sets the text alignment. Alignment is: Jtextfield.left, Jtextfield.center, Jtextfield.right.
    • Requestfocus (), set focus.
    • 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.
    • Removeactionlistener (ActionListener) Removes the text box monitor.
    • 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 a text box in the specified number of characters.
    • getPreferredSize () returns the size that the text box would like to have.
    • getPreferredSize (int), returns the size of the text box that you want to have in the specified number of characters.

The example 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 access rights are not set (view source files).

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:

    • Getechochar (), returns the Echo character of the password.
    • Setechochar (char) to 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:

    • Declares a text area name.
    • Creates a text area object.
    • Adds a text area object to a container.

The main construction method of the JTextArea class:

    • JTextArea () to create a text area object with the default number of columns and rows.
    • JTextArea (String s) that creates a text area object with S as its initial value.
    • JTextArea (Strings, int x,int y), with S as the initial value, the number of rows x and the number of columns Y, creates a text area object.
    • JTextArea (int x,int y) creates a text area object with the number of rows x and the number of columns Y.

Other common ways to JTextArea classes:

    • SetText (String s), sets the display text, and clears the original text.
    • GetText (), gets the text of the text area.
    • Insert (String S,int x) inserts the specified text at the specified location.
    • Replace (String s,int x,int y), with the given one to replace the text ending at the x position to the Y position.
    • Append (String s) appends text to the text area.
    • Getcareposition (), gets the position of the active cursor in the text area.
    • setcareposition (int n), set the position of the active cursor.
    • Setlinewrap (Boolean B), sets the line wrap, 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

In the GUI, common text boxes and text areas implement data input and output. If you use text area input, you usually set up a separate data entry completion button. When the data entry is finished, click the button. An event handler uses the GetText () method to read string information from the text area. For cases where the text box is input, the last entered return character can trigger the input completion event, usually without a separate button. Event handlers can use the Word Analyzer to analyze a number of numbers, and then use the string to convert the numerical method to obtain the input value. For output, the program converts the numeric value to a string, and then outputs the data to a text box or text area by SetText () method.

The example small application sets a text area, a text box, and two buttons. The user enters a sequence of integers in the text area and clicks the Sum button, which sums the sequence of integers in the text area and outputs the sum in the text box. Click the second button to clear the text area and the contents of the text box.

Import Java.util.*;import Java.applet.*;import java.awt.*;
Import Javax.swing.*;import java.awt.event.*; public class J509 extends Applet implements actionlistener{JTextArea texta; JTextField TEXTF;
  JButton b1,b2;
    public void init () {setSize (250,150);
    Texta=new JTextArea ("", 5,10);
    Texta.setbackground (Color.cyan);
    Textf=new JTextField ("", 10);
    Textf.setbackground (Color.pink); B1=new JButton ("sum");
    B2=new JButton ("re-start");
    Textf.seteditable (FALSE); B1.addactionlistener (this);
    B2.addactionlistener (this); Add (texta); Add (TEXTF);
  Add (B1); add (B2);
      public void actionperformed (ActionEvent e) {if (E.getsource () ==b1) {String s=texta.gettext ();
      StringTokenizer tokens=new StringTokenizer (s);
      Use the default delimiter collection: Spaces, wrapping, tab compliance with carriage return as delimiters int n=tokens.counttokens (), sum=0,i;
      for (i=0;i<=n-1;i++) {String temp=tokens.nexttoken ();//Remove a data sum+=integer.parseint (temp) from the text area;
    } textf.settext ("" +sum); } elseif (E.getsource () ==b2) {texta.settext (null);
    Textf.settext (NULL);
 }
  }
}

The

Example applet calculates all the numbers that are factor multiples from the starting integer to the terminating integer. The applet container uses the GridLayout layout to divide the interface into 3 rows, the first row is the label, the second and third rows are two panel. Two panel container class Panel1,panel2 are designed and divided by GridLayout layout respectively. Panel1 is 1 rows, 6 columns, and Panel2 is 1 rows, 4 columns. The label and container class Panel1,panel2 components are then added to the corresponding position in the window.

Import Java.applet.*;import javax.swing.*;
Import Java.awt.*;import java.awt.event.*;
  Class Panel1 extends jpanel{JTextField text1,text2,text3; Panel1 () {//constructor method.
    When a panel object is created, the panel is initialized with three tags//three text boxes, arranged as GridLayout (1,6) text1=new JTextField (10);
    Text3=new JTextField (a); SetLayout (new GridLayout (1,6));
    Add (New JLabel ("Starting number", jlabel.right); add (Text1);
    Add (New JLabel ("Termination number", jlabel.right)); add (Text2);
  Add (New JLabel ("factor", Jlabel.right)); add (TEXT3); } class Panel2 extends jpanel{//extended Panel class JTextArea text;
  JButton Button; Panel2 () {//constructor method.
    When a panel object is created, the panel is initialized with a label//text box, arranged as GridLayout (1,4) text=new JTextArea (4,10); Text.setlinewrap (true);
    JScrollPane jsp=new JScrollPane (text);
    Button=new JButton ("Start calculation");
    SetLayout (New GridLayout (1,4));
    Add (new JLabel ("Calculated result:", jlabel.right));
    Add (JSP);
  Add (New Label ()), add (Button); } public class J510 extends Applet implements actionlistener{Panel1 Panel1; PANEL2 Panel2;
    public void init () {setlayout (new GridLayout (3,1));
    SetSize (400,200);p anel1=new Panel1 ();p anel2=new Panel2 ();
    Add (New JLabel ("calculates number of factor multiples from start to end number", Jlabel.center));
    Add (Panel1); add (Panel2); (Panel2.
  Button). addActionListener (this); The public void actionperformed (ActionEvent e) {if e.getsource () = = (Panel2.
      Button)) {long n1,n2,f,count=0;
      N1=long.parselong (Panel1.text1.getText ());
      N2=long.parselong (Panel1.text2.getText ());
      F=long.parselong (Panel1.text3.getText ());
      for (long i=n1;i<=n2;i++) {if (i%f==0) panel2.text.append (string.valueof (i) + ""); }
    }
  }
}

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.