import java.awt.BorderLayout;
public class JFramedemo extends JFrame {
protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
private JPanel contentPane;
private JTextField textField;
private JScrollPane scrollPane;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFramedemo frame = new JFramedemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFramedemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 656, 433);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
showdir();
}
}
});
textField.setBounds(54, 10, 344, 27);
textField.setFont(new Font("宋体", Font.PLAIN, 18));
contentPane.add(textField);
textField.setColumns(30);
JButton btnNewButton = new JButton("转到");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showdir();
}
});
btnNewButton.setBounds(403, 10, 113, 27);
contentPane.add(btnNewButton);
scrollPane = new JScrollPane();
scrollPane.setBounds(54, 50, 462, 323);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
public void showdir() {
String str = textField.getText();
File file = new File(str);
if(file.exists() && file.isDirectory())
{
textArea.setText("");
File file2[] = file.listFiles();
for(File file3 : file2)
{
textArea.append(file3.getName() + LINE_SEPARATOR);
}
}
}
}
原文:https://www.cnblogs.com/WINDZLY/p/11854389.html