//建立一个简单的窗口能够实现:列出指定目录内容的基本功能。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class AwtDemo2
{
private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
private Dialog dl;
private Label la;
private Button b1;
AwtDemo2()
{
FrameCreate();
}
public void FrameCreate()
{
f = new Frame("文件");
f.setSize(500,600);
f.setLocation(200,300);
f.setLayout(new FlowLayout());
b=new Button("确定");
tf=new TextField(30);
ta=new TextArea(30,50);
f.add(tf);
f.add(b);
f.add(ta);
dl=new Dialog(f,"提示信息",true);
dl.setBounds(300,400,250,100);
dl.setLayout(new FlowLayout());
la = new Label();
b1 = new Button("确定");
dl.add(la);
dl.add(b1);
eventCreate();
f.setVisible(true);
}
private void ShowDir()
{
String dir = tf.getText();
File fi = new File(dir);
if(fi.exists()&&fi.isDirectory())
{
ta.setText("");
String[] name = fi.list();
for(String sname:name)
{
ta.append(sname+"\r\n");
}
}
else
{
String ssname="您输入的信息:"+dir+"错误,请重新输入!";
la.setText(ssname);
dl.setVisible(true);
}
}
public void eventCreate()
{
tf.addKeyListener(new KeyAdapter()//为什么我这儿的这个Enter键功能总是实现不了呢?????
//好像是我加的这个事件,它的代码就没有运行。。。
{
public void KeyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
ShowDir();
}
}
});
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent v)
{
dl.setVisible(false);
}
});
dl.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dl.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ShowDir();
}
});
}
public static void main(String[] args)
{
AwtDemo2 ad=new AwtDemo2();
}
}
|
|