📄 filesdisplay.java
字号:
package com.incesoft.keywordcategory;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class FilesDisplay extends JFrame{
/**
* @param args
*/
JTree filetree;
JScrollPane jdirp;
JScrollPane rightpane;
JTextPane jshowp;
JSplitPane jsplitp;
JSplitPane leftsplit;
JSplitPane rightsplit;
JTextField searchwordtext;
JButton searchbutton;
JPanel leftpane;
JPanel searchpane;
HashMap<String,ArrayList<String>> fileContentHashMap;
JPanel p;
JTable resulttable;
public void init(){
setTitle("词库检索");
File file = new File("dic");
fileContentHashMap=new HashMap<String,ArrayList<String>>();
DefaultMutableTreeNode root=new DefaultMutableTreeNode(file.getName());
DisplayFiles(file,root);
filetree=new JTree(root);
filetree.addMouseListener(new MouseAdapter()
{
public void mouseClicked( MouseEvent me)
{
doMouseClicked(me);
}
});
searchwordtext=new JTextField("",10);
searchbutton=new JButton("搜索");
searchwordtext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ee) {
// TODO Auto-generated method stub
searchbutton.doClick();
}
});
searchbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
ArrayList<String> arrList=fileContentHashMap.get(searchwordtext.getText());
if(arrList!=null){
String[] columnNames={"文件名","文件路径"};
final Object[][] data=new Object[arrList.size()][];
for(int i=0;i<arrList.size();i++){
data[i]=new Object[2];
String s=arrList.get(i);
data[i][0]=s.substring(s.lastIndexOf('\\')+1);
data[i][1]=s;
}
resulttable=new JTable(data,columnNames);
resulttable.setEnabled(false);
resulttable.setSize(resulttable.getPreferredSize());
p=new JPanel(new GridLayout(0,1));
rightsplit=new JSplitPane(JSplitPane.VERTICAL_SPLIT,resulttable,p);
rightpane.getViewport().add(rightsplit);
resulttable.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
int clickedrow=resulttable.rowAtPoint(new Point(e.getX(),e.getY()));
try {
BufferedReader bufreader=new BufferedReader(new FileReader((String)data[clickedrow][1]));
String context="";
String s=bufreader.readLine();
while(s!=null)
{
context+=s+"\n";
s=bufreader.readLine();
}
p.add(jshowp);
jshowp.setText(context);
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
}
else {
rightpane.getViewport().add(jshowp);
jshowp.setText("Not find the word");
}
}
});
searchpane=new JPanel();
searchpane.add(searchwordtext,BorderLayout.CENTER);
searchpane.add(searchbutton,BorderLayout.WEST);
jdirp=new JScrollPane(filetree,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
leftpane=new JPanel();
leftpane.add(jdirp,BorderLayout.CENTER);
leftpane.add(searchpane,BorderLayout.SOUTH);
leftsplit=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jdirp,searchpane);
jshowp=new JTextPane();
jshowp.setEditable(false);
rightpane=new JScrollPane();
jsplitp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftsplit,rightpane);
jsplitp.setOneTouchExpandable(true);
add(jsplitp);
setSize(400,500 );
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screen.width-getSize().width)/2, (screen.height-getSize().height)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void DisplayFiles(File file,DefaultMutableTreeNode root) {
File[] files;
if(file.isDirectory()){
files=file.listFiles();
for(int i=0;i<files.length;i++){
DefaultMutableTreeNode ChildNode=new DefaultMutableTreeNode(files[i].getName());
root.add(ChildNode);
DisplayFiles(files[i],ChildNode);
}
}
else {
String filepath=file.getPath();
try {
BufferedReader bufreader=new BufferedReader(new FileReader(filepath));
String s=bufreader.readLine();
while(s!=null)
{
if(fileContentHashMap.containsKey(s)){
fileContentHashMap.get(s).add(filepath);
}
else{
ArrayList<String> arrList = new ArrayList<String>();
arrList.add(filepath);
fileContentHashMap.put(s,arrList);
}
s=bufreader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
void doMouseClicked(MouseEvent me){//双击树中的一个文件后打开此文件
TreePath tp=filetree.getPathForLocation(me.getX(), me.getY());
if(tp!=null){
Object[] pathobjects=tp.getPath();
int len=pathobjects.length,i;
String path="";
for(i=0;i<len-1;i++)
path+=pathobjects[i].toString()+"/";
path+=pathobjects[i].toString();
File clickfile=new File(path);
if(clickfile.isFile()){
try {
BufferedReader bufreader=new BufferedReader(new FileReader(clickfile));
String context="";
String s=bufreader.readLine();
while(s!=null)
{
context+=s+"\n";
s=bufreader.readLine();
}
rightpane.getViewport().add(jshowp);
jshowp.setText(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new FilesDisplay().init();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -