In a graphical interface, text boxes and text areas are components for information input and output.
Text box
A text box (JTextField) is a box in the interface that is used to input and output a line of text. JTextField class is used to create text boxes. The interface associated with text boxes is ActionListener.
The basic content of the text box handler is as follows:
The main constructor methods of JTextField class:
Other methods of JTextField class:
[Example] The applet has two text boxes. One text is used to enter an integer, and another text box displays the squared value of this integer. The program uses a string to convert the basic type method Long.parseLong(text1.getText()), reads the string in text box text1, and converts it into 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, the variables in the main class are declared as class variables and access permissions are not set (see the source file).
The password box (JPasswordField) is a single-line input component, which is basically similar to JTextField. The password box has an additional blocking function, which means that when input, it will be output as another specified character (usually * character). In addition to the text box methods introduced earlier, there are some commonly used methods for password boxes:
Text area
The text area (JTextArea) is an area in the form where text is placed. The main difference between a text area and a text box is that the text area can store multiple lines of text. The JTextArea class in the javax.swing package is used to create text areas. There are no events for the JTextArea component.
The basic content of the text area processing program is as follows:
The main constructor methods of JTextArea class:
Other common methods of the JTextArea class:
The following code creates a text area and sets it to automatically wrap it.
JTextArea textA = new JTextArea("I am a text area", 10, 15); textA.setLineWrap(true);//Set automatic line wrap
When there is a lot of content in the text area and cannot be displayed in the text area, you can attach a scroll bar to the text area. Setting a scrollbar for the text area allows the following code:
JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta);//Add scrollbars to the text area
In the GUI, commonly used text boxes and text areas implement data input and output. If you use text area input, you usually set another data input completion button. When the data input is finished, click this button. The event handler uses the getText() method to read string information from the text area. For situations where text boxes are used as input, the last input carriage return character can inspire the input completion event, usually without setting another button. The event handler can use the word analyzer to analyze numbers, and then use the string conversion method to obtain the input numerical value. For output, the program first converts the value into a string, and then outputs the data to a text box or text area through the setText() method.
[Example] The applet sets a text area, a text box and two buttons. The user enters an integer sequence in the text area, clicks the sum button, the program sums the integer sequence in the text area, and outputs the sum in the text box. Click the second button to clear the contents in the text area and text boxes.
import java.util.*;import java.applet.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class J509 extends App let 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 JTe xtField("",10); textF.setBackground(Color.pink); b1=new JButton("Sum"); b2=new JButton("Restart"); textF.setEditable(false); b1.addActionListener(this); b 2.addActionListener(this ); add(textA); add(textF); add(b1); add(b2); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b1){ String s=textA.get Text( ); StringTokenizer tokens=new StringTokenizer(s); //Use the default separator set: spaces, line breaks, Tab conform to carriage return as separator int n=tokens.countTokens(),sum=0,i; for(i= 0;i<=n-1;i++){ String temp=tokens.nextToken();//Pick a data sum+=Integer.parseInt(temp); } textF.setText(""+sum); } else if(e.getSource()==b2){ textA.setText(null); textF.setText(null); } }}
[Example] The applet calculates all numbers from the starting integer to the ending integer that are factor multiples. The applet container uses GridLayout layout to divide the interface into 3 rows, the first row is the label, and the second row and the third row are two Panels. Two Panel container classes Panel1 and Panel2 are designed, and they are divided by GridLayout layout. Panel1 has 1 row and 6 columns, and Panel2 has 1 row and 4 columns. Then add the components generated by the label and container classes Panel1 and Panel2 to the corresponding position of the window.
import java.applet.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;class Panel1 extends JPanel{ JTextField text1,text2,t ext3; Panel1(){//Construction method . When creating a Panel object, the Panel is initialized to have three labels //three text boxes, with the layout as 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("terminal number", JLabel.RIGHT)) ;add(text2); add(new JLabel("factor",JLabel.RIGHT));add(text3); }}class Panel2 extends JPanel{//Extend Panel class JTextArea text;JButton Button; Panel2 (){// Constructing method. When creating a Panel object, the Panel is initialized to have a tag // a text box, with the layout as GridLayout(1,4) text=new JTextArea(4,10); text.setLineWrap(true); JScrollPane jsp=new JScroll Pane( text); Button=new JButton("Start calculation"); setLayout(new GridLayout(1,4)); add(new JLabel("Computation 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 Gr idLayout(3,1)); setSize(400,200);panel1= new Panel1();panel2=new Panel2(); add(new JLabel("calculate the number from the start number to the end number 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)+""); } } }}