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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 徐鹏辰 于 2015-7-16 22:58 编辑
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. class WindowDemo
  5. {
  6.         private Frame f;
  7.         private Button but;
  8.         private TextField tf;
  9.         private TextArea ta;

  10.         private Dialog d;
  11.         private Label l;
  12.         private Button okBut;
  13.         WindowDemo()
  14.         {
  15.                 init();
  16.         }
  17.         public void init()
  18.         {
  19.                 //设置窗口基本信息
  20.                 f=new Frame("我的电脑");
  21.                 but=new Button("转到");
  22.                 tf=new TextField(50);
  23.                 ta=new TextArea(20,60);
  24.                 f.setBounds(200,100,500,400);
  25.                 //将组件添加到Frame中
  26.                 f.add(tf);
  27.                 f.add(but);
  28.                 f.add(ta);
  29.                 //设置布局
  30.                 f.setLayout(new FlowLayout());
  31.                 //显示窗体
  32.                 f.setVisible(true);        
  33.                 event();
  34.                
  35.                 //设置对话框基本信息
  36.                 d=new Dialog(f,"提示信息",true);
  37.                 d.setBounds(240,150,200,150);
  38.                 l=new Label();
  39.                 okBut=new Button("确定");
  40.                 //添加组件
  41.                 d.add(l);
  42.                 d.add(okBut);
  43.                 //设置布局
  44.                 d.setLayout(new FlowLayout());
  45.                
  46.         }
  47.         private void event()
  48.         {
  49.                 //窗口监听:退出
  50.                 f.addWindowListener(new WindowAdapter()
  51.                 {
  52.                         public void windowClosing(WindowEvent e)
  53.                         {
  54.                                 System.exit(0);
  55.                         }
  56.                 });
  57.                 d.addWindowListener(new WindowAdapter()
  58.                 {
  59.                         public void windowClosing(WindowEvent e)
  60.                         {
  61.                                 d.setVisible(false);
  62.                         }
  63.                 });

  64.                
  65.                 okBut.addActionListener(new ActionListener()
  66.                 {
  67.                         public void actionPerformed(ActionEvent e)
  68.                         {
  69.                                 d.setVisible(false);
  70.                         }
  71.                 });
  72.                

  73.                 //按钮监听
  74.                 but.addActionListener(new ActionListener()
  75.                 {
  76.                         public void actionPerformed(ActionEvent e)
  77.                         {
  78.                                 String dirPath=tf.getText();
  79.                                 File dir=new File(dirPath);
  80.                                 if(dir.exists()&&dir.isDirectory())
  81.                                 {
  82.                                         ta.setText("");
  83.                                         String []names=dir.list();
  84.                                         for(String name:names)
  85.                                         {
  86.                                                 ta.append(name+"\r\n");
  87.                                         }
  88.                                 }
  89.                                 else
  90.                                 {
  91.                                         String info="您输入的信息:"+dirPath+"是错误的";
  92.                                         l.setText(info);
  93.                                         //显示对话框
  94.                                         d.setVisible(true);        
  95.                                 }
  96.                         }
  97.                 });
  98.         }
  99.         public static void main(String[] args)
  100.         {
  101.                 new WindowDemo();
  102.         }
  103. }
复制代码


9 个回复

倒序浏览
哥们,看了你好久的代码,终于找到问题的所有了
在init()方法中,你将对话框信息的设置放在了event()方法的后面,所有导致了event()方法在执行时对话框没有初始化,而产生了空指针异常。

QQ截图20150716214126.png (103 KB, 下载次数: 8)

QQ截图20150716214126.png

评分

参与人数 1黑马币 +2 收起 理由
徐鹏辰 + 2 赞一个!

查看全部评分

回复 使用道具 举报 1 0

回帖奖励 +2

还没学到这边   正在学习面向对象  先看一下
回复 使用道具 举报
wing-SF 发表于 2015-7-16 21:42
哥们,看了你好久的代码,终于找到问题的所有了
在init()方法中,你将对话框信息的设置放在了event()方法 ...

问题解决了,谢谢了
回复 使用道具 举报
第一次发帖,不知道怎么分配奖励!:L
回复 使用道具 举报
没看懂,还没有学习到这里来
回复 使用道具 举报
徐鹏辰 发表于 2015-7-16 22:54
问题解决了,谢谢了

写代码一定要写注释啊,不然看起来真的真的真的。。。。。
回复 使用道具 举报

回帖奖励 +2

有点晕
回复 使用道具 举报
对GUI不是很了解,学习下
回复 使用道具 举报
wing-SF 发表于 2015-7-19 09:22
写代码一定要写注释啊,不然看起来真的真的真的。。。。。

嗯嗯,谢谢,知道了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马