我写了个Keywords.java类,代码如下:
*************************************************************
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class Keywords extends JFrame {
String[] keywords = { "abstract", "boolean", "break", "byte", "case",
"catch", "char", "class", "continue", "default", "do", "double",
"else", "extends", "false", "final", "finally", "float", "for",
"if", "implements", "import", "instanceof", "int", "interface",
"long", "native", "new", "null", "package", "private", "protected",
"public", "return", "short", "static", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "true",
"try", "void", "volatile", "while", "const", "goto" };
JTextArea text;
JTextArea result;
public Keywords() {
this.setTitle("计算关键字数");
// 文本框
text = new JTextArea(6, 50);
text.setLineWrap(true);
JScrollPane textScroll = new JScrollPane(text);
text.setBorder(BorderFactory.createBevelBorder(1));
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.setBorder(BorderFactory.createTitledBorder("导入的文本"));
textPanel.add(textScroll);
// 结果框
result = new JTextArea(6, 50);
result.setLineWrap(true);
JScrollPane resultScroll = new JScrollPane(result);
result.setBorder(BorderFactory.createBevelBorder(1));
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.setBorder(BorderFactory.createTitledBorder("计算结果"));
resultPanel.add(resultScroll);
// 导入文本和结果框
JPanel allPanel = new JPanel();
allPanel.setLayout(new GridLayout(2, 1));
allPanel.add(textPanel);
allPanel.add(resultPanel);
// 按钮
JButton impButton = new JButton("导入文本");
JButton calcButton = new JButton("计算关键字数");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(impButton);
buttonPanel.add(calcButton);
// 添加
this.add(allPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
// this.setSize(400, 300);
this.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 导入文本
impButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
writeText();
}
});
// 计算关键字数
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// for(String ss : s) {
// System.out.println(ss);
// }
String[] start = text.getText().trim().split("\n");
System.out.println("start: " + start.length);
// String[] rets = new String[start.length];
StringBuffer ret = new StringBuffer();
for (int i = 0; i < start.length; i++) {
StringTokenizer st = new StringTokenizer(start[i]);
int n = 0;
while (st.hasMoreTokens()) {
String comp = st.nextToken();
for (String s : keywords) {
if (s.equals(comp)) {
n++;
if (n == 1) {
ret.append(i + ": ");
ret.append(s);
} else {
ret.append(", " + s);
}
}
}
}
if (n != 0) {
ret.append("\n");
}
}
result.setText(ret.toString().trim());
}
});
}
// 导入文本
public void writeText() {
try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
File file = chooser.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String s;
text.setText("");
while ((s = br.readLine()) != null) {
text.append(s + "\n");
}
text.setText(text.getText().trim());
System.out.println(text.getText());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new Keywords();
}
}
*************************************************************
运行结果如下:
词法编译器,用C写过,找找
我写了个Keywords.java类,代码如下:
*************************************************************