获取Frame对象
获取TextField对象
获取TextArea对象
获取Button对象
调用Frame对象的add()方法,添加进去
调用TextField对象的getText()方法,可以获取文本框内的数据
调用TextArea对象的setText()方法,设置文本数据
列目录
获取到文本框中的路径,包装成File对象
调用File对象的list()方法,可以得到String[]文件名数组
import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; public class FrameDemo { private Frame frame; private TextField tf; private TextArea ta; private Button button; public FrameDemo() { init(); } /** * 初始化 */ public void init() { frame = new Frame("测试窗体"); frame.setBounds(300, 200, 300, 400); frame.setLayout(new FlowLayout()); tf = new TextField(20); button = new Button("转到"); ta = new TextArea(30, 30); frame.add(button); frame.add(tf); frame.add(ta); frame.setVisible(true); addEventAction(); } /** * 添加事件 */ public void addEventAction() { // 退出 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); // action事件 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String dirName = tf.getText(); File file = new File(dirName); if (!file.isDirectory() || !file.exists()) { ta.setText("目录不存在"); return; } String[] files = file.list(); StringBuilder sb = new StringBuilder(); for (String name : files) { sb.append(name); sb.append("/r/n"); } ta.setText(sb.toString()); } }); } /** * @param args */ public static void main(String[] args) { new FrameDemo(); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/industrynews/12639.html