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");
}
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]) {
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);
}
});
}
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);
}
}