java代码词频 java词频统计( 四 )


resultLabel.setFont(resultLabel.getFont().deriveFont(60f));
add(openBtn, "North");
add(resultLabel, "Center");
JPanel southPane = new JPanel(new BorderLayout());
southPane.add(textField, "Center");
southPane.add(calcBtn, "East");
add(southPane, "South");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (this.openBtn == source) {
if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(this)) {
file = fileChooser.getSelectedFile();
this.openBtn.setText(file.getName());
this.openBtn.setEnabled(false);
this.calcBtn.setEnabled(true);
}
} else if (this.calcBtn == source) {
this.calcBtn.setEnabled(false);
String word = this.textField.getText().trim();
if (word.isEmpty()) {
return;
}
Scanner in = null;
try {
in = new Scanner(file);
int count = 0;
while (in.hasNext()) {
if (word.equals(in.next())) {
count++;
}
}
this.resultLabel.setText("" + count);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally {
if (in != null) {
in.close();
System.out.println("Scanner关闭");
}
}
this.calcBtn.setEnabled(true);
this.openBtn.setEnabled(true);
this.openBtn.setText("选择文件");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestWin().setVisible(true);
}
});
}
}
关于java代码词频和java词频统计的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读