本帖最后由 凝聚 于 2013-11-26 19:29 编辑
package twenty_two;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Ten {
public static void main(String[] args) {
new Mwinn();
}
}
class Mwinn
{
private Frame f;
private TextField tf;
private Button but;
private Button okbut;
private TextArea ta;
private Label lae;
private Dialog dg;
Mwinn()
{
init();
}
public void init()
{
f=new Frame("中");
f.setBounds(300,200,500,999);
f.setLayout(new FlowLayout());
tf=new TextField(30);
but=new Button("转到");
ta=new TextArea(15,40);
dg=new Dialog(f,"提示信息—self",true);
dg.setBounds(500,200,300,160);
dg.setLayout(new FlowLayout());
lae=new Label();
okbut=new Button("确定");
dg.add(lae);
dg.add(okbut);
f.add(tf);
f.add(but);
f.add(ta);
mevent();
f.setVisible(true);
}
private void mevent()
{
okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dg.setVisible(false);
}});
tf.addKeyListener( new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER);
showDir();
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
dg.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dg.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void showDir()
{
String dirPath=tf.getText();
File dir=new File(dirPath);//D:\\java\\workspace\\heima\\src\\twenty_two
if(dir.exists()&&dir.isDirectory())
{
ta.setText("");//不加它两次目录的东西都会存在。
String[]names=dir.list();
for(String name :names)
{
ta.append(name+"\r\n");//将所有的文件都输出来。
}
}
else
{
String info="您的路径为:"+ dirPath+"这是错误的路径";
lae.setText(info);
dg.setVisible(true);
}
}
}
这段程序运行后,我在对话框中粘贴一个路径,可以运行,但自己写一个路径,(“没等我写完路径”)就会弹出错误提示,这是怎么回事?
|
|