Java implements input and output for text boxes and text areas

  • 2020-04-01 03:54:57
  • OfStack

In a GUI, text boxes and text areas are often used to implement the input and output of data. If text field input is used, an additional data entry completion button is usually set. When the data entry is finished, click this button. The event handler USES the getText() method to read the string information from the text area. In the case of a text box, a carriage return at the end of the input can fire the input completion event, usually without a separate button. The event handler can use the word analyzer to analyze a number, and then use the string conversion method to get the input value. For the output, the program converts the value to a string and then outputs the data to a text box or text area through the setText() method.

The applet sets up a text area, a text box, and two buttons. The user enters the sequence of integers in the text area, clicks the sum button, the program 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 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(" o   and "); b2=new JButton(" Start all over again ");
    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 delimiters: space, line feed, Tab line return as delimiter
      int n=tokens.countTokens(),sum=0,i;
      for(i=0;i<=n-1;i++){
        String temp=tokens.nextToken();//Take 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);
    }
  }
}

Applet calculates all Numbers that are multiples of factors from the starting integer to the ending integer. The applet container USES the GridLayout layout to divide the interface into 3 rows and columns, the first row is the label, the second and third rows are the two panels. Design two Panel container classes, Panel1 and Panel2, and divide them by GridLayout layout. Panel1 has 1 row and 6 columns, and Panel2 has 1 row and 4 columns. Then add the label and container classes Panel1 and Panel2 to the corresponding location 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. When a Panel object is created, the Panel is initialized to have three labels
    //Three text boxes, GridLayout(1,6)
    text1=new JTextField(10);text2=new JTextField(10);
    text3=new JTextField(10);setLayout(new GridLayout(1,6));
    add(new JLabel(" Starting number ",JLabel.RIGHT));add(text1);
    add(new JLabel(" Termination of the number of ",JLabel.RIGHT));add(text2);
    add(new JLabel(" factor ",JLabel.RIGHT));add(text3);
  }
}
class Panel2 extends JPanel{//Extend the Panel class
  JTextArea text;JButton Button;
  Panel2(){//Constructor. When a Panel object is created, the Panel is initialized to have a label
    //A text box, GridLayout(1,4)
    text=new JTextArea(4,10);text.setLineWrap(true);
    JScrollPane jsp=new JScrollPane(text);
    Button=new JButton(" Start counting ");
    setLayout(new GridLayout(1,4));
    add(new JLabel(" Calculation results: ",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 that is a factor multiple from the beginning to the end ",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)+"");
      }
    }
  }
}

The above is all the content of this article, I hope you can enjoy it.


Related articles: