- /*
- 简单的窗口,实现文件目录的遍历
- */
- //导包
- import java.awt.event.*;
- import java.awt.*;
- import java.io.*;
- class FrameTest {
- //对窗口进行设置之
- Frame f =new Frame();
- Button but = new Button("点我啊");
- TextField tf = new TextField(60);
- TextArea ta = new TextArea(25,70);
- FrameTest(){
- f.setBounds(300,100,600,500);
- f.setLayout(new FlowLayout());
- f.add(tf);
- f.add(but);
- f.add(ta);
- myEvent();
- f.setVisible(true);
- }
- //定义触发事件和对事件的处理
- public void myEvent(){
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- but.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- String text = tf.getText();
- File dir = new File(text);
- //对目录的遍历
- if (dir.exists()&&dir.isDirectory()){
- ta.setText("");
- String[] names = dir.list();
- for (String name:names ){
- ta.append(name+"\r\n");
- }
- }
- tf.setText("");
- }
- });
- }
- public static void main(String[] args) {
- FrameTest ft = new FrameTest();
-
- }
- }
复制代码
|
|