📄 jm.java
字号:
package 读取关键字soft29;
import java.awt.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Jm extends JFrame {
private JTextArea textArea;
/**
* @param args
*/
String Filepath;
String keyword[]={"cout","cin","for","main","while","public","return"};
int KeyWordCount[];
public static void main(String[] args)
{
new Jm();
}
public Jm() {
super("关键字查询");
this.setSize(525,400);
getContentPane().setLayout(null);
final JButton 打开文件 = new JButton();
打开文件.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
FileDialog fd=new FileDialog(Jm.this);//添加浏览文件的面板
fd.setVisible(true);//显示面板
String str=fd.getDirectory()+fd.getFile();
if (!str.equals("") &&str!=null &&(str.endsWith(".cpp")||str.endsWith(".h")) )
{Filepath=str;
textArea.append(readFile());}
else
//JOptionPane.showConfirmDialog(Jm.this, "输入尤物");
JOptionPane.showMessageDialog(Jm.this, "输入出错 请输入cpp或.h文件");
}
});
打开文件.setText("打开文件");
打开文件.setBounds(70, 40, 101, 25);
getContentPane().add(打开文件);
final JButton 寻找关键字 = new JButton();
寻找关键字.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
comp( textArea);
}
});
寻找关键字.setText("寻找关键字");
寻找关键字.setBounds(330, 40, 101, 25);
getContentPane().add(寻找关键字);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(70, 124, 358, 177);
getContentPane().add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
this.setVisible(true);
}
String readFile(){
File file=new File(Filepath);
if (file.exists()){
try{
BufferedReader in=new BufferedReader(new FileReader(file));
char buf[]=new char[10000];
in.read(buf);
return String.valueOf( buf).trim();
//return in.readLine();
}catch(Exception e){}
}
return "sorry";
}
public void comp(JTextArea t)
{
String str;
KeyWordCount=new int[keyword.length];
File file=new File(Filepath);
if(!file.exists())
t.append("没有找到文件");
else
{
try
{
BufferedReader in =new BufferedReader(new FileReader(file));
int j=1;
while((str=in.readLine())!=null)
{
str=repStr(str);
if (!str.equals(""))
{
String split[]=str.split(" ");
for(int i=0;i<split.length;i++)
{
split[i]=split[i].trim();
if ( verifyKeyWord( split[i] ) )
{
t.append("在第"+(j)+"行找到关键字"+split[i]+"\n");
}
}
}
j++;
}
for (int i=0;i<keyword.length;i++){
if (KeyWordCount[i]>0)
t.append("关键字 "+keyword[i]+" 出现次数:"+KeyWordCount[i]+"\n");
}
}
catch(Exception e){t.append("出错");}
}
}
boolean verifyKeyWord(String s){
for (int j=0;j<keyword.length;j++){
if(s.equals(keyword[j])){
KeyWordCount[j]++;
return true;
}
}
return false;
}
public String repStr(String s)
{
String str="";
for (int i=0;i<s.length();i++){
char c=s.charAt(i);
if ( c>=65 && c<=90 ||c>=97 && c<=122 )
str+=c;
else
str+=' ';
}
return str.trim();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -