package com.tz.util;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class MyWindowDemo {
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
MyWindowDemo() {
}
public void init() {
f = new Frame("my window");
f.setBounds(200, 300, 500, 600);
f.setLayout(new FlowLayout());
tf = new TextField(30);
but = new Button("跳转");
ta = new TextArea(15, 40);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
System.exit(0);
}
});
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String text = tf.getText();
ta.setText(text);
tf.setText(null);
// 读取文件目录
String dirPath = tf.getText();
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
ta.setText(null);
String[] names = dir.list();
for (String name : names) {
ta.append(name + "\r\n");
}
}else{
Dialog dl=new Dialog(f,true);
dl.setTitle("error");
Label lb=new Label();
lb.setVisible(true);
dl.add(lb);
lb.setText(dirPath+"路径错误");
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}