美文网首页
2021-09-23 GUI(练习-列出指定目录内容)

2021-09-23 GUI(练习-列出指定目录内容)

作者: Denholm | 来源:发表于2021-10-23 09:49 被阅读0次
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

public class MyWindowDemo {

    private Frame f;
    private Button btn;
    private TextField textField;
    private TextArea textArea;

    public MyWindowDemo() {
        init();
    }

    private void init() {
        f = new Frame("my window");
        f.setBounds(300, 100, 600, 500);
        f.setLayout(new FlowLayout());
        textField = new TextField(60);
        btn = new Button("转到");
        textArea = new TextArea(25, 70);
        f.add(textField);
        f.add(btn);
        f.add(textArea);
        myEvent();
        f.setVisible(true);
    }

    private void myEvent() {

        btn.addActionListener(e -> {
            String dirPath = textField.getText();
            File file = new File(dirPath);
            if (file.exists() && file.isDirectory()) {
                textArea.setText("");
                String[] list = file.list();
                for (String name : list) {
                    textArea.append(name + "\r\n");
                }
            }
            textField.setText("");
        });

        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

        });

    }

    public static void main(String[] args) {
        new MyWindowDemo();
    }

}

相关文章

网友评论

      本文标题:2021-09-23 GUI(练习-列出指定目录内容)

      本文链接:https://www.haomeiwen.com/subject/bqaogltx.html