package gui.awt;
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.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class MyWindow{
private Frame frame;
private TextField tf1;
private TextArea ta1;
private Button zhuan;
private Dialog dialog;
private Label label;
private Button ok;
public MyWindow(){
init();
}
public void init(){
frame=new Frame("转换列表");
tf1=new TextField(55);
ta1=new TextArea(20,65);
zhuan=new Button("转到");
frame.add(tf1);
frame.add(zhuan);
frame.add(ta1);
frame.setBounds(100,100,500,400);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
dialog=new Dialog(frame,"这是一个对话框",true);
label=new Label();
ok=new Button("确定");
dialog.setBounds(200,200,200,150);
dialog.add(label);
dialog.add(ok);
dialog.setLayout(new FlowLayout());
event();
}
public void event(){
//Frame窗体的事件添加
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//转到摁扭的事件添加
zhuan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
toDo();
}
});
tf1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
toDo();
}
}
});
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
tf1.setText("");
ta1.setText("");
dialog.setVisible(false);
}
});
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
tf1.setText("");
ta1.setText("");
dialog.setVisible(false);
}
});
}
public void toDo(){
String dirPath=tf1.getText();
File file=new File(dirPath);
if(file.exists()&&file.isDirectory()){
ta1.setText("");//每次清空
File[]ff=file.listFiles();
for(File fff:ff){
ta1.append(fff.getAbsolutePath()+"\r\n");//追加字符串,不会覆盖之前的字符串,追加到末尾处
}
// ta1.setText(dirPath);//每次setText,都会把之前的覆盖掉
}else{
label.setText("错误路径:"+dirPath+"请重新输入");
dialog.setVisible(true);
}
}
public static void main(String[] args) {
new MyWindow();
}
}
黑马程序员——AWT+IO获取文件列表
最新推荐文章于 2021-03-12 15:19:26 发布