【JAVA】高频单词统计器(附教程)

视频教程.

package EngLishPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class Note implements ActionListener {
	private MyFrame _myFrame;
	private JLabel _label;
	private JButton _openBtn;
	private JButton _saveBtn;
	private JTextArea _textArea;
	public static HashMap<String, Integer> map = new HashMap<String, Integer>();
	public static String ss[];
	public static int value[];
	public static int p;

	public static void in(String s) {
		String t = "";
		s = ";" + s + ";";
		int p = 0;
		for (int i = 1; i < s.length(); i++) {
			if (isab(s.charAt(i)) && (!isab(s.charAt(i - 1)))) {
				p = i;
			}
			if (isab(s.charAt(i)) && (!isab(s.charAt(i + 1)))) {
				t = s.substring(p, i + 1);
				map.put(t, map.containsKey(t) ? map.get(t) + 1 : 1);
			}
		}
	}

	public static boolean isab(char c) {
		return c >= 'a' && c <= 'z';
	}

	public Note() {
		_myFrame = new MyFrame("高频英文单词统计器——王跃坤QQ群323348662");
		_myFrame.setSize(960, 560);
		_myFrame.setLayout(new BorderLayout());
		// 初始化组件
		this.initComponents();
		_myFrame.setVisible(true);
	}

	public static String tot(String s) {
		for (int i = s.length(); i < 20; i++) {
			s += " ";
		}
		return s;
	}

	private void initComponents() {
		JPanel labelP = new JPanel();
		_label = new JLabel("当前没有打开任何文件");
		_label.setFont(new Font("楷体", Font.BOLD, 40));
		labelP.add(_label);
		_myFrame.add(labelP, BorderLayout.NORTH);

		JPanel contentP = new JPanel();
		_textArea = new JTextArea();
		_textArea.setLineWrap(true);
		_textArea.setFont(new Font("楷体", Font.BOLD, 20));
		_myFrame.add(new JScrollPane(_textArea), BorderLayout.CENTER);

		JPanel btnP = new JPanel(new GridLayout(1, 2));
		_openBtn = new JButton("打开源文本文件");
		_saveBtn = new JButton("输出高频词文件");
		_openBtn.addActionListener(this);
		_saveBtn.addActionListener(this);
		_openBtn.setFont(new Font("楷体", Font.BOLD, 30));
		_saveBtn.setFont(new Font("楷体", Font.BOLD, 30));
		btnP.add(_openBtn);
		btnP.add(_saveBtn);
		_myFrame.add(btnP, BorderLayout.SOUTH);
		_textArea.setCaretColor(Color.yellow);
		_textArea.setDisabledTextColor(Color.RED);
		_textArea.setSelectedTextColor(Color.blue);
		_textArea.setSelectionColor(Color.green);

		_textArea.append("请选择打开源文本文件" + "\n");
	}

	/** ActionListener接口方法 */
	public void actionPerformed(ActionEvent e) {
		File file = null;
		JFileChooser fileChooser = new JFileChooser();// 打开根目录(默认是用户)
		if (e.getSource() == _openBtn) { // 点击了打开按钮
			fileChooser.setDialogTitle("打开文件");
			fileChooser.setApproveButtonText("确定");
			int result = fileChooser.showOpenDialog(_myFrame);
			if (result == JFileChooser.APPROVE_OPTION) { // 点击了确定按钮
				file = fileChooser.getSelectedFile();
				_label.setText("打开的文件为:" + file.getName());
			} else if (result == JFileChooser.CANCEL_OPTION) { // 点击了取消按钮
				_label.setText("没有选择任何文件");
			} else {
				_label.setText("操作出现错误");
			}

			if (file != null) {
				try {
					Scanner scanner = new Scanner(new FileInputStream(file));
					_textArea.append("\n" + "正在引入文件请稍候..." + "\n" + "\n");
					while (scanner.hasNext()) {
						String string = scanner.next();
						string = string.toLowerCase();
						in(string);
					}
					ss = new String[map.size()];
					value = new int[map.size()];
					p = 0;
					for (String s : map.keySet()) {
						ss[p] = s;
						value[p] = map.get(s);
						p++;
					}
					int t;
					String tt;
					for (int i = 0; i < p; i++) {
						for (int j = 0; j < p - i - 1; j++) {
							if (value[j] < value[j + 1]) {// 乱序
								// if((value[j]<value[j+1])||((value[j]==value[j+1])&&ss[j].compareTo(ss[j+1])>0)){有序
								t = value[j];
								value[j] = value[j + 1];
								value[j + 1] = t;
								tt = ss[j];
								ss[j] = ss[j + 1];
								ss[j + 1] = tt;
							}
						}
					}
					scanner.close();
					_textArea.append("文件读入完成" + "\n" + "\n" + "请输出高频词文件" + "\n" + "\n");
				} catch (Exception ex) {
					_label.setText("文件读取出错");
				}
			}
		} else { // 点击了保存按钮

			fileChooser.setDialogTitle("保存文件");
			fileChooser.setApproveButtonText("确定");
			int result = fileChooser.showSaveDialog(_myFrame);
			if (result == JFileChooser.APPROVE_OPTION) { // 点击了确定按钮
				file = fileChooser.getSelectedFile();
				_label.setText("选择存储的文件为:" + file.getName());
			} else if (result == JFileChooser.CANCEL_OPTION) { // 点击了取消按钮
				_label.setText("没有选择任何文件");
			} else {
				_label.setText("操作出现错误");
			}

			if (file != null) {
				try {
					PrintStream writename = new PrintStream(new FileOutputStream(file));
					int index = 1;
					for (int i = 0; i < p; i++) {
						if (ss[i].length() > 1) {
							writename.print(index + " : " + tot(ss[i]) + "\t" + value[i] + "\n");
							index += 1;
						}
					}
					writename.flush();
					_textArea.append("高频词文件输出完成!" + "\n" + "\n" + "-------------------" + "\n");
					_textArea.append("请选择文件或关闭程序" + "\n" + "-------------------" + "\n");
					map.clear();
					p = 0;
				} catch (Exception ex) {
					_label.setText("保存文件失败");
				}
			}
		}
	}

	public static void main(String[] args) {
		new Note();
	}
}

class MyFrame extends JFrame {
	public MyFrame() {
		super();
	}

	public MyFrame(String title) {
		super(title);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(1);
			}
		});
	}

	/** 重写setSize方法 */
	public void setSize(int width, int height) {
		super.setSize(width, height);

		int screenW = (int) this.getToolkit().getScreenSize().getWidth();
		int screenH = (int) this.getToolkit().getScreenSize().getHeight();
		this.setLocation((screenW - width) / 2, (screenH - height) / 2);
	}
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王跃坤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值