输入路径(如d:/用户目录)列举路径下的文件(夹)
代码
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.File; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class 目录查找 { protected static final String LINE_SEPARATOR = System .getProperty("line.separator"); public 目录查找(JFrame f) { final JTextField text = new JTextField(30); final JButton b = new JButton("确定"); final JTextArea area = new JTextArea(); f.setTitle("查找目录下的文件"); f.setBounds(100, 100, 500, 500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); JPanel p1 = new JPanel(); p1.add(text); p1.add(b); f.add(p1, BorderLayout.NORTH); f.add(new JScrollPane(area), BorderLayout.CENTER); area.setEditable(false); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String str_dir = text.getText(); File file = new File(str_dir); if (file.exists() && file.isDirectory()) { area.setText(null); String[] name = file.list(); for (String s : name) area.append(s + LINE_SEPARATOR); } } }); text.addKeyListener(new KeyAdapter() {//添加键盘监听 @Override public void keyPressed(KeyEvent arg0) { if (arg0.getKeyChar() == KeyEvent.VK_ENTER) { b.doClick(); } } }); } public static void main(String[] args) { 目录查找 f = new 目录查找(new JFrame()); } }