?? fmain.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 + -