学习中,遇到点小问题。我模仿着做了个窗体,窗体中有一个文本框、一个按钮、一个文本区域。
在文本框中输入文件路径,点击回车键或按按钮,如果有该路径则在文本区域中显示该路径下的文件或目录名。
测试结果很奇怪:
//疑问:很奇怪,我输入的D盘(d:)和(d:\)效果竟然不一样,其他盘出来的效果一致。怎么回事?
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class MyWindowDemo {
private Frame frame = null;
private Button btn = null;
private TextField tf = null;
private TextArea ta = null;
private Dialog dl = null;
private Button dlBtn = null;
private Label label = null;
public static void main(String[] args) {
new MyWindowDemo();
}
MyWindowDemo() {
init();
}
public void init() {
frame = new Frame("我的小窗");
// 设置的是什么?
// tf.setSize(400,20);
// 设置列数20 列数70
ta = new TextArea(20, 70);
tf = new TextField(60);
btn = new Button("转到");
frame.setLayout(new FlowLayout());
frame.setBounds(200, 100, 600, 400);
btn.setSize(50, 20);
frame.add(tf);
frame.add(btn);
frame.add(ta);
myEvent();
frame.setVisible(true);// 此句必须在最后
}
private void myEvent() {
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getFileNames();
/*
* File dir = new File(dirPath); if(dir.exites() &&
* dir.isDirectory()){ ta.setText(""); String[] names =
* dir.list(); for(String name:names){ //需要改成\r \n \r\n都可以换行
* ta.append(name+"\r\n"); } }
*/
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e);
getFileNames();
}
});
}
private void myDialogEvent() {
if (dlBtn != null)
dlBtn.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
dl.setVisible(false);
}
});
if (dl != null)
dl.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dl.setVisible(false);
}
});
}
private void getFileNames() {
ta.setText("");
String text = tf.getText();
// ta.setText(text);
File dir = new File(text);
if (dir.isDirectory() && dir.exists()) {
String[] fileNames = dir.list();
for (String fileName : fileNames) {
ta.append(fileName + "\r\n");
}
} else if (dir.isFile() && dir.exists()) {
ta.setText(tf.getText());
tf.setText("");
} else if (!dir.exists()) {
dl = new Dialog(frame, "文件提示对话框", true);
dlBtn = new Button("okButton");
label = new Label("找不到: " + text + " 。请确认地Internet或地址正确!!");
// 注意:必须放到前面 dl.setVisible(true)前
myDialogEvent();
dl.setLayout(new FlowLayout());
dl.add(label);
dl.add(dlBtn);
dl.setBounds(300, 400, 500, 200);
dl.setVisible(true);
}
}
}