zoukankan      html  css  js  c++  java
  • Java知多少(86)文本框和文本区的输入输出

    在GUI中,常用文本框和文本区实现数据的输入和输出。如果采用文本区输入,通常另设一个数据输入完成按钮。当数据输入结束时,点击这个按钮。事件处理程序利用getText()方法从文本区中读取字符串信息。对于采用文本框作为输入的情况,最后输入的回车符可以激发输入完成事件,通常不用另设按钮。事件处理程序可以利用单词分析器分析出一个个数,再利用字符串转换数值方法,获得输入的数值。对于输出,程序先将数值转换成字符串,然后通过setText()方法将数据输出到文本框或文本区。

    【例 11-9】小应用程序设置一个文本区、一个文本框和两个按钮。用户在文本区中输入整数序列,单击求和按钮,程序对文本区中的整数序列进行求和,并在文本框中输出和。单击第二个按钮,清除文本区和文本框中的内容。

     1 import java.util.*;import java.applet.*;import java.awt.*;
     2 import javax.swing.*;import java.awt.event.*;
     3 public class J509 extends Applet implements ActionListener{
     4     JTextArea textA;JTextField textF;JButton b1,b2;
     5     public void init(){
     6         setSize(250,150);
     7         textA=new JTextArea("",5,10);
     8         textA.setBackground(Color.cyan);
     9         textF=new JTextField("",10);
    10         textF.setBackground(Color.pink);
    11         b1=new JButton("求 和"); b2=new JButton("重新开始");
    12         textF.setEditable(false);
    13         b1.addActionListener(this); b2.addActionListener(this);
    14         add(textA); add(textF); add(b1);add(b2);
    15     }
    16     public void actionPerformed(ActionEvent e){
    17         if(e.getSource()==b1){
    18             String s=textA.getText();
    19             StringTokenizer tokens=new StringTokenizer(s);
    20             //使用默认的分隔符集合:空格、换行、Tab符合回车作分隔符
    21             int n=tokens.countTokens(),sum=0,i;
    22             for(i=0;i<=n-1;i++){
    23                String temp=tokens.nextToken();//从文本区取下一个数据
    24                sum+=Integer.parseInt(temp);
    25             } 
    26             textF.setText(""+sum);
    27         }
    28         else if(e.getSource()==b2){
    29             textA.setText(null);
    30             textF.setText(null);
    31         }
    32     }
    33 }

    【例 11-10】小应用程序计算从起始整数到终止整数中是因子倍数的所有数。小程序容器用GridLayout布局将界面划分为3行列,第一行是标签,第二行和第三行是两个Panel。设计两个Panel容器类Panel1,Panel2,并分别用GridLayout布局划分。Panel1为1行6列,Panel2为1行4列。然后将标签和容器类Panel1,Panel2产生的组件加入到窗口的相应位置中。

     1 import java.applet.*;import javax.swing.*;
     2 import java.awt.*;import java.awt.event.*;
     3 class Panel1 extends JPanel{
     4     JTextField text1,text2,text3;
     5     Panel1(){//构造方法。当创建Panel对象时,Panel被初始化为有三个标签
     6         //三个文本框,布局为GridLayout(1,6)
     7         text1=new JTextField(10);text2=new JTextField(10);
     8         text3=new JTextField(10);setLayout(new GridLayout(1,6));
     9         add(new JLabel("起始数",JLabel.RIGHT));add(text1);
    10         add(new JLabel("终止数",JLabel.RIGHT));add(text2);
    11         add(new JLabel("因子",JLabel.RIGHT));add(text3);
    12     }
    13 }
    14 class Panel2 extends JPanel{//扩展Panel类
    15     JTextArea text;JButton Button;
    16     Panel2(){//构造方法。当创建Panel对象时,Panel被初始化为有一个标签
    17         //一个文本框,布局为GridLayout(1,4)
    18         text=new JTextArea(4,10);text.setLineWrap(true);
    19         JScrollPane jsp=new JScrollPane(text);
    20         Button=new JButton("开始计算");
    21         setLayout(new GridLayout(1,4));
    22         add(new JLabel("计算结果:",JLabel.RIGHT));
    23         add(jsp);
    24         add(new Label());add(Button);
    25     }
    26 }
    27 public class J510 extends Applet implements ActionListener{
    28     Panel1 panel1;Panel2 panel2;
    29     public void init(){
    30         setLayout(new GridLayout(3,1));
    31         setSize(400,200);panel1=new Panel1();panel2=new Panel2();
    32         add(new JLabel("计算从起始数到终止数是因子倍数的数",JLabel.CENTER));
    33         add(panel1);add(panel2);
    34         (panel2.Button).addActionListener(this);
    35     }
    36     public void actionPerformed(ActionEvent e){
    37         if(e.getSource()==(panel2.Button)){
    38             long n1,n2,f,count=0;
    39             n1=Long.parseLong(panel1.text1.getText());
    40             n2=Long.parseLong(panel1.text2.getText());
    41             f=Long.parseLong(panel1.text3.getText());
    42             for(long i=n1;i<=n2;i++){
    43                 if(i%f==0)
    44                 panel2.text.append(String.valueOf(i)+"");
    45            }
    46         }
    47     }
    48 }

    系列文章:

  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/Coda/p/4561782.html
Copyright © 2011-2022 走看看