A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 凝聚 中级黑马   /  2013-11-26 15:18  /  1344 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 凝聚 于 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);
        }
}
}
这段程序运行后,我在对话框中粘贴一个路径,可以运行,但自己写一个路径,(“没等我写完路径”)就会弹出错误提示,这是怎么回事?

3 个回复

倒序浏览
楼主代码有误,多了一段代码:
tf.addKeyListener( new KeyAdapter()
        {
                public void keyPressed(KeyEvent e)
                {
                        if(e.getKeyCode()==KeyEvent.VK_ENTER);
                        showDir();
                }
        });
这段代码楼主监听了tf的输入状态,所以当你输入以个字符马上触发事件,所以不会让你输入完就报错,
解决完毕,呵呵
回复 使用道具 举报
可是这段代码试管,当按下enter见时触发弹出事件,或吧输入的字符输入到下面的框中。 所以这段代码不能丢啊!
回复 使用道具 举报

向楼主敬礼,呵呵,好样

我是来刷墙的


















防辐射孕妇装
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马