<span style="font-family: 微软雅黑; font-size: 14px; line-height: 30.7999992370605px;">------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------</span>
</pre><pre name="code" class="java">//列出指定目录内容
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{//先把界面涉及到的组件定义出来
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
MyWindowDemo()
{
}
public void init()
{
f = new Fram("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,70);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
private void myEvent();
{//匿名内部类
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists()&&dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for (String name:name)
{
ta.append(name+"\r\n");
}
}
tf.setText("");
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyWindowDemo();
}