import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class MyWindowDemo {
private Frame f;
private Button btn;
private TextField textField;
private TextArea textArea;
public MyWindowDemo() {
init();
}
private void init() {
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
textField = new TextField(60);
btn = new Button("转到");
textArea = new TextArea(25, 70);
f.add(textField);
f.add(btn);
f.add(textArea);
myEvent();
f.setVisible(true);
}
private void myEvent() {
btn.addActionListener(e -> {
String dirPath = textField.getText();
File file = new File(dirPath);
if (file.exists() && file.isDirectory()) {
textArea.setText("");
String[] list = file.list();
for (String name : list) {
textArea.append(name + "\r\n");
}
}
textField.setText("");
});
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new MyWindowDemo();
}
}
网友评论