本帖最后由 唐杨 于 2012-8-24 08:45 编辑
楼主,我已经帮你修改好了,在我这里已经能够正常运行了.
望采纳.你写错的地方我已经用红色标识出来了. /*
列出指定目录内容
*/
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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
class MyWindowDemo {
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;// 新建一个对话框
private Label lab;// 新建一个标签
private Button okBut;
MyWindowDemo() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25, 70);
d = new Dialog(f, "提示信息-self", true);// 新建对话框
d.setBounds(400, 200, 240, 150);
d.setLayout(new FlowLayout());
lab = new Label();// 新建标签
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
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);
}
});
okBut.addActionListener(new ActionListener() { //这里的okBut你写成okBuf了,同时按钮要添加的是动作监听器,而不是窗口监听器
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
d.setVisible(false);
}
});
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 : names) {
ta.append(name + "\r\n");
}
} else {
d.setVisible(true);
}
// public void windowClosing(WindowEvent e)
// {
// System.exit(0);
// }
}
});
}
} //楼主之前这里多了一个大括号,我已经去掉了
class LabelTest {
public static void main(String[] args) {
new MyWindowDemo();
}
}
附:运行图:
|