62,628
社区成员
发帖
与我相关
我的任务
分享
package com.swing.demo;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyWindow {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyWindow window = new MyWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MyWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 872, 611);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(44, 41, 589, 52);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("\u8F6C\u5230");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*
* 通过点击按钮获取文件框中的内容
* 将目录中的内容显示在文本区域中
*
*/
String dir_str = textField.getText();
File dir = new File(dir_str);
if(dir.exists() && dir.isDirectory()) {
String[] names = dir.list();
for(String name :names) {
textArea.append(name);
}
}
}
});
btnNewButton.setBounds(669, 41, 113, 52);
frame.getContentPane().add(btnNewButton);
JTextArea textArea = new JTextArea();
textArea.setBounds(40, 133, 720, 360);
frame.getContentPane().add(textArea);
}
}
