⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textareacount.java

📁 统计一个文本域中行、单词、字符的数量。 提示:在图形界面中除了安排文本域输入数据外
💻 JAVA
字号:
package com.david;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.regex.Pattern;
class MyActionListener implements ActionListener{
    private TextArea text1;
    private TextField text2;
    private TextField text3;
    private TextField text4;
    public MyActionListener(TextArea text1,TextField text2,TextField text3,TextField text4){
        // TODO Auto-generated constructor stub
        this.text1 = text1;
        this.text2 = text2;
        this.text3 = text3;
        this.text4 = text4;
    }
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        int k = 0;
        int count = 0;
        int temp = 0;
        int i = 0;
        String text = text1.getText();
        while(true){
            k=text.indexOf("\r\n",k+1);
            if(k != -1)
                count++;
            else break;
        }
        text2.setText(Integer.toString(count));
        String str = text.trim();
        //str.replaceAll("\\s*"," ");
        String[] words =str.split("\\s");
        k = words.length;
        for(int j = 0;j < words.length;j++){
            if(words[j].equals(""))k--;
        }
        text3.setText(Integer.toString(k));
        for(int j = 0;j < words.length;j++){
        	
            temp = temp+words[j].length();
        }
        text4.setText(Integer.toString(temp));
    }
}
public class TextAreaCount extends Frame{
    //MyActionListener action ;
    public TextAreaCount() {
        // TODO Auto-generated constructor stu
        init();
    }
    public void init(){
    	   //Frame f= new Frame("统计文本单词,行数,字母");
         setLayout(new GridLayout(5,2));
         TextArea text = new TextArea(null,100,60,TextArea.SCROLLBARS_BOTH);
         Button button = new Button("统计");
         Label label1 = new Label("行数");
         Label label2 = new Label("单词");
         Label label3 = new Label("字母");
         TextField textfield1 = new TextField(5);
         TextField textfield2 = new TextField(5);
         TextField textfield3 = new TextField(5);
         add(text);
        add(button);
        add(label1);
        add(textfield1);
        add(label2);
        add(textfield2);
        add(label3);
        add(textfield3);
        button.addActionListener(new MyActionListener(text,textfield1,textfield2,textfield3));
        addWindowListener(new WindowAdapter() 
        		{        public void windowClosing(WindowEvent e)
        		       {          
        		         System.exit(0); 
        		       }
        		});
          }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TextAreaCount textAreaCount = new TextAreaCount();
        textAreaCount.setSize(400,200);
        textAreaCount.setLocation(400,400);
        textAreaCount.setTitle("统计文本行数,单词数目,字母数");
        //textAreaCount.pack();
        textAreaCount.show();
    }

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -