⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fmain.java

📁 java本地文件搜索,功能较简单,实现思想也较简单
💻 JAVA
字号:
package src;

import java.awt.Button;
import java.awt.Component;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FMain extends JFrame {


        /**
         * Launch the application
         * @param args
         */
        public static void main(String args[]) {
                try {
                        FMain frame = new FMain();
                        frame.setVisible(true);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        /**
         * Create the frame
         */
        public FMain() {
                  this.setTitle("文件搜索器(应聘展示)");
          this.setSize(600, 400);
          this.setLocationRelativeTo(null);
                  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  this.add(new SearchPanel());
        }
}
        class SearchPanel extends JPanel
        {
                private JLabel lcondition,lpath,lstate;
                private JTextField fcondition,fpath;
                private Button search,open;
                private JTextArea area;
                private File selectFile;
                private int count;
                private String c,p;
                public SearchPanel(){
                        lcondition= new JLabel("查询条件");
                        lpath=new JLabel("查询目录");
                        lstate = new JLabel("准备查找...");

                        fcondition=new JTextField(20);
                        fpath=new JTextField(15);
                        area=new JTextArea();
                        area.setEditable(false);
                        JScrollPane sp=new JScrollPane(area);
                        search=new Button("查询");
                        open=new Button("打开");
                        search.addActionListener(new ActionListener(){
                                public void actionPerformed(ActionEvent e){
                                        count=0;
                                        area.setText("");
                                        c=fcondition.getText().trim();
                                        p=fpath.getText().trim();
                                        if("".equals(c)){
                                                lstate.setText("查询条件不能为空");
                                        }
                                        else if("".equals("p")){
                                                lstate.setText("查询目录不能为空");
                                        }
                                        else{
                                                Thread t=new Thread(new Runnable(){
                                                        public void run(){
                                                                fSearch(c,p);
                                                        }
                                                });
                                                t.start();
                                        }
                                }
                        });
                        open.addActionListener(new ActionListener()
                          {
                           public void actionPerformed(ActionEvent e)
                           {
                            JFileChooser jfc = new JFileChooser();  //文件选择器
                            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //选择模式为目录

                            int r = jfc.showDialog(null, "浏览");
                            if (r == JFileChooser.APPROVE_OPTION)
                            {
                             selectFile = jfc.getSelectedFile();
                             fpath.setText(selectFile.getPath());
                            }
                           }
                          });

                         setLayout(new GridBagLayout());
                          GridBagConstraints gc = new GridBagConstraints();

                          gc.anchor = GridBagConstraints.WEST;
                          addComponet(gc, lcondition, 0, 0, 1, 1);
                          addComponet(gc, lpath, 0, 1, 1, 1);

                          gc.fill = GridBagConstraints.HORIZONTAL;
                          gc.weightx = 100;
                          addComponet(gc, fcondition, 1, 0, 1, 1);
                          addComponet(gc, fpath, 1, 1, 1, 1);

                          gc.fill = GridBagConstraints.NONE;
                          gc.weightx = 0;
                          addComponet(gc, search, 2, 0, 1, 1);
                          addComponet(gc, open, 2, 1, 1, 1);

                          gc.fill = GridBagConstraints.HORIZONTAL;
                          gc.weightx = 100;
                          addComponet(gc, lstate, 0, 2, 3, 1);

                          gc.fill = GridBagConstraints.BOTH;
                          gc.weighty = 100;
                          addComponet(gc, sp, 0, 3, 3, 1);

                }
                public void addComponet(GridBagConstraints gc,Component c,int x,int y,int w,int h){
                        gc.gridx=x;
                        gc.gridy=y;
                        gc.gridheight=h;
                        gc.gridwidth=w;
                        add(c,gc);
                }
                public void fSearch(String condition,String path){
                        File f=new File(path);
                        if(f.exists()){
                                if(f.isDirectory()){
                                        File[] fs=f.listFiles();
                                        for(File file:fs){
                                                lstate.setText(file.getPath());
                                                if(file.getName().equals(condition)){
                                                        count++;
                                                        area.append(file.getPath()+"\r\n");
                                                }
                                                else{
                                                        if(file.isDirectory()){

                                                        fSearch(condition,file.getPath());
                                                        }
                                                }
                                        }
                                        lstate.setText("查找结束,共"+ count +"个文件");
                                }
                                else{
                                        lstate.setText("目录无法打开!!!");
                                }
                        }
                        else{
                                lstate.setText("查找目录错误!!!");
                        }
                }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -