展开全部
我写了个Keywords.java类,代62616964757a686964616fe78988e69d8331333262356132码如下:
*************************************************************
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
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();
}
}
*************************************************************
运行结果如下:
今天的文章java 关键字提取_提取Java关键字分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7320.html