Home>

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 for inputting and outputting a line of text in the interface.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:

Declare a text box name. Create a text box object. Adds a text box object to a container. Register the monitor for the text box object that needs to be controlled,Listen for the end of text box input (that is, enter the enter key) event. A method that handles text box events,Complete the judgment and processing of the interception event.

The main constructor of the jtextfield class:

jtextfield (), the length of the text box is 1. jtextfield (int columns), the initial value of the text box is an empty string,The text box's character length is set to columns. jtextfield (string text), the initial value of the text box is a text string. 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:

setfont (font f), set font settext (string text), set the text in the text box gettext (), Get the text in the text box. seteditable (boolean), specifies the editability of the text box,The default is true and editable. sethorizontalalignment (int alignment) Sets the text alignment.The alignment methods are:jtextfield.left, jtextfield.center, jtextfield.right. requestfocus (), sets the focus. addactionlistener (actionlistener), set the action monitor for the text box,Specifies that the actionlistener object receives input end action events that occur on this text box. removeactionlistener (actionlistener) removes a text box monitor. getcolumns (), returns the number of columns in the text box. getminimumsize (), returns the minimum required size of the text box. getminimumsize (int), returns the text box with the specified number of characters,Required minimum size. getpreferredsize (), returns the desired size of the text box. getpreferredsize (int), returns the text box with the specified number of characters,Desired size.

[Example] The applet has two text boxes.A text for entering an integer,Another text box displays the squared value of this integer.The program uses the string to basic type method long.parselong (text1.gettext ()) to read the string in the text box text1.And convert it to an integer.The program uses an instance of the sqr class as a monitor,But in order for the monitor to access the variables of the main class,Variables in the main class are declared as class variables,And no access permissions are set (view source files).

The password field (jpasswordfield) is a single-line input component.Basically similar to jtextfield.The password box has an additional blocking function,Is when typingWill be output with another specified character (usually * character). In addition to the text box method described earlier,There are other common methods for password boxes:

getechochar (), returns the echo character of the password. setechochar (char), set the echo character of the password box.

Text area

A text area (jtextarea) is an area on the 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 the text area processing program has the following aspects:

Declare a text area name. Create a text area object. Adds a text area object to a container.

The main constructor of the jtextarea class:

jtextarea (), with the default number of columns and rows,Create a text area object. jtextarea (string s), with s as the initial value,Create a text area object. jtextarea (strings, int x, int y), with s as the initial value,Create a text area object with x rows and y columns. 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:

settext (string s), set the display text,Also clear the original text. gettext (), Get the text of the text area. insert (string s, int x), insert the specified text at the specified position. replace (string s, int x, int y), replace the text from the x position to the y position with the given one. append (string s), append text to the text area. getcareposition (), Get the position of the active cursor in the text area. setcareposition (int n), sets the position of the active cursor. setlinewrap (boolean b), set word wrapBy default,No word wrapping.

The following code creates a text area,And set to wrap automatically.

 jtextarea texta=new jtextarea ("I am a text area", 10,15);
  texta.setlinewrap (true);//Set word wrap

When there is more content in the text area,When you cannot display all of the text area,You can add scroll bars to the text area.The following code can be used to set the scroll bar in the text area:

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

In gui, commonly used text boxes and text areas implement data input and output.If using text area input,There is usually another data input completion button.When data entry ends,Click this button.The event handler uses the gettext () method to read string information from the text area.For text boxes as input,The last carriage return entered can trigger an input completion event,There is usually no need for a separate button.An event handler can analyze a number using a word analyzer,Then use the string conversion numeric method,Get the entered value.For output,The program first converts the value into a string.Then use the settext () method to output the data to a text box or text area.

[Example] The small application sets a text area, a text box, and two buttons.The user enters a sequence of integers into the text area,Click the sum button,The program sums the sequence of integers in the text area,And output the sum in the text box.Click the second button,Clear the contents of the text area and 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 ("Restart");
    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 set of separators:spaces, line feeds, and tabs that match the carriage return as separators
      int n=tokens.counttokens (), sum=0, i;
      for (i=0;i<= n-1;i ++) {
        string temp=tokens.nexttoken ();//Retrieve the next data from the text area
        sum +=integer.parseint (temp);
      }
      textf.settext ("" + sum);
    }
    else if (e.getsource () == b2) {
      texta.settext (null);
      textf.settext (null);
    }
  }
}

[Example] The applet calculates all numbers that are multiples of the factor from the starting integer to the ending integer.The applet container uses a gridlayout layout to divide the interface into 3 rows and columns. The first row is the label.The second and third rows are two panels. Design two panel container classes, panel1 and panel2, and divide them with gridlayout.panel1 is 1 row and 6 columns, and panel2 is 1 row and 4 columns. Then add the components generated by the label and container classes panel1 and panel2 to the corresponding positions of the window.

import java.applet. *;import javax.swing. *;
import java.awt. *;import java.awt.event. *;
class panel1 extends jpanel {
  jtextfield text1, text2, text3;
  panel1 () {//Construction method.
When a panel object is created, the panel is initialized to have three labels
    //Three text boxes,Layout is gridlayout (1,6)
    text1=new jtextfield (10);text2=new jtextfield (10);
    text3=new jtextfield (10);setlayout (new gridlayout (1,6));
    add (new jlabel ("start number", jlabel.right));add (text1);
    add (new jlabel ("terminated number", jlabel.right));add (text2);
    add (new jlabel ("factor", jlabel.right));add (text3);
  }
}
class panel2 extends jpanel {//Extends the panel class
  jtextarea text;jbutton button;
  panel2 () {//Construction method.
When a panel object is created, the panel is initialized to have a label
    //a text box,Layout is 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);panel1=new panel1 ();panel2=new panel2 ();
    add (new jlabel ("Calculate the number from the start to the end is a factor multiple", jlabel.center));
    add (panel1);add (panel2);
    (panel2.button) .addactionlistener (this);
  }
  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) + "");
      }
    }
  }
}
  • Previous In-depth analysis of some applications oriented to byte streams in Java programming
  • Next A small note of the mysql master-slave configuration solution