package collection;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Demo4{
/**
* @练习
*
*/
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
Demo4(){
init();
}
public void init(){
f = new Frame("MyWindow");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());//流布局
tf = new TextField(29);
but = new Button("change to:");
ta = new TextArea(25,40);
d = new Dialog(f,"information_self",true);
d.setBounds(400, 200, 250, 100);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("Yes");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent(){
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
okBut.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
showDir();
}
});
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void showDir(){
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory()){
ta.setText("");
String[] names = dir.list();
for(String name: names){
ta.append(name+"\r\n");
}
}
else{
String info = "The input \" "+dirPath +" \" is wrong, please input again!";
lab.setText(info);
d.setVisible(true);
}
tf.setText("");
}
public static void main(String[] args) {
new Demo4();
}
}