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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马-王双 中级黑马   /  2013-6-22 23:27  /  1915 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 一个窗体中含有一个单行文本框
  3. 一个按钮和一个文本域
  4. 功能:在文本框输入一个路径,比如c:\
  5. 就会在文本域中显示C盘下的所有目录
  6. 如果输入路径不存在,会弹出一个对话框
  7. 提示输入有误。
  8. 出现的问题:输入路径错误时,对话框会弹出,
  9. 但是点击确定按钮或者对话框的关闭按钮都不能让对话框消失
  10. 也就是后两个事件发生后未被处理,该怎么办?
  11. 要求:对话框Dialog对象一定只在输入有误时才建立,不要定义在外部
  12. */
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.io.*;
  16. class MyWindow
  17. {
  18. private Frame f;
  19. private Button but;
  20. private TextField tf;
  21. private TextArea ta;
  22. MyWindow()
  23. {
  24. init();
  25. }
  26. public void init()
  27. {
  28. f=new Frame("my window");
  29. f.setBounds(300,100,600,500);
  30. f.setLayout(new FlowLayout());
  31. tf=new TextField(60);
  32. but=new Button("转到");
  33. ta=new TextArea(25,70);
  34. f.add(tf);
  35. f.add(but);
  36. f.add(ta);
  37. myEvent();
  38. f.setVisible(true);
  39. }
  40. public void myEvent()
  41. {
  42. f.addWindowListener(new WindowAdapter()
  43. {
  44. public void windowClosing(WindowEvent e)
  45. {
  46. System.exit(0);
  47. }
  48. });
  49. //从键盘输入enter或是按“转到”按钮功能相同
  50. tf.addKeyListener(new KeyAdapter()
  51. {
  52. public void keyPressed(KeyEvent e)
  53. {
  54. if(e.getKeyCode()==KeyEvent.VK_ENTER)
  55. showDir();
  56. }
  57. });
  58. but.addActionListener(new ActionListener()
  59. {
  60. public void actionPerformed(ActionEvent e)
  61. {
  62. showDir();
  63. }
  64. });
  65. }
  66. public void showDir()
  67. {

  68. //获取文本
  69. String dirPath=tf.getText();
  70. /*
  71. 将单行文本框中的数据填充到文本域
  72. ta.setText(dirPath);
  73. 清空文本框
  74. tf.setText("");
  75. */
  76. //封装成文件对象
  77. File dir=new File(dirPath);
  78. //如果dir文件对象存在且是目录
  79. if (dir.exists()&&dir.isDirectory())
  80. {
  81. //清空文本域
  82. ta.setText("");
  83. String[] names=dir.list();
  84. for (String name:names )
  85. {
  86. ta.append(name+"\r\n");
  87. /*如果使用setText方法,
  88. 会使后一次的结果覆盖掉前一次的结果
  89. ta.setText(name+"\r\n");
  90. */
  91. }
  92. }
  93. else
  94. {
  95. //错误提示对话框d被定义在内部,
  96. //是希望只在信息输入有误时才创建对象
  97. final Dialog d=new Dialog(f,"错误提示",true);
  98. d.setBounds(500,200,240,150);
  99. d.setLayout(new FlowLayout());
  100. Label lab=new Label();
  101. Button diaBut=new Button("确定");
  102. d.add(lab);
  103. d.add(diaBut);
  104. String info="您输入的:"+dirPath+"路径有误,请重输";
  105. lab.setText(info);
  106. d.setVisible(true);
  107. //这两个事件该放在什么位置
  108. diaBut.addActionListener(new ActionListener()
  109. {
  110. public void actionPerformed(ActionEvent e)
  111. {
  112. d.setVisible(false);
  113. }
  114. });
  115. d.addWindowListener(new WindowAdapter()
  116. {
  117. public void windowClosing(WindowEvent e)
  118. {
  119. d.setVisible(false);
  120. }
  121. });

  122. }
  123. }
  124. public static void main(String[] args)
  125. {
  126. new MyWindow();
  127. }
  128. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

4 个回复

倒序浏览
已经帮楼主把程序改好了,详细说明请看代码中的注释:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. class MyWindow
  5. {
  6. //有关窗体的对象引用
  7.         private Frame f;
  8.         private Button but;
  9.         private TextField tf;
  10.         private TextArea ta;
  11. //有关对话框的对象引用
  12.         private Dialog d;
  13.         private Label lab;
  14.         private Button diaBut;
  15. /*
  16.         这里只是引用哦,并没有new对象,
  17.         只是起个名字方便以后全局调用
  18. */


  19. //---------------------构造函数---------------------------
  20.         MyWindow()  
  21.         {
  22.                 init();
  23.         }
  24. //---------------------初始化函数--------------------------

  25.         public void init()
  26.         {
  27.                 f=new Frame("my window");
  28.                 f.setBounds(300,100,600,500);
  29.                 f.setLayout(new FlowLayout());

  30.                 tf=new TextField(60);

  31.                 but=new Button("转到");

  32.                 ta=new TextArea(25,70);

  33.                 f.add(tf);
  34.                 f.add(but);
  35.                 f.add(ta);

  36.                 frameEvent();

  37.                 f.setVisible(true);
  38.         }

  39. //-----------------------------窗体的事件函数----------------------
  40.        
  41.         public void frameEvent()  //窗体的窗体监听器
  42.         {
  43.                 f.addWindowListener(new WindowAdapter()
  44.                 {
  45.                         public void windowClosing(WindowEvent e)
  46.                         {
  47.                                 System.exit(0);
  48.                         }
  49.                 });

  50.        

  51.                 tf.addKeyListener(new KeyAdapter()  //文本框的键盘监听器,用于直接用回车键确认
  52.                 {
  53.                         public void keyPressed(KeyEvent e)
  54.                         {
  55.                                 if(e.getKeyCode()==KeyEvent.VK_ENTER)
  56.                                 showDir();
  57.                         }
  58.                 });

  59.                 but.addActionListener(new ActionListener()  //按钮的事件监听器
  60.                 {
  61.                         public void actionPerformed(ActionEvent e)
  62.                         {
  63.                                 showDir();
  64.                         }
  65.                 });

  66.         }
  67. //------------------------对话框的事件函数-----------------------------------
  68. /*
  69.         因为窗体创建的时候对话框的对象还没有创建,所以不能把这些事件和上面窗体的事件写到一起,
  70.         否则初始化函数init调用的时候会报空指针异常。
  71. */
  72.         public void dialogEvent()/
  73.         {
  74.        
  75.                 diaBut.addActionListener(new ActionListener()        //对话框按钮的事件监听器
  76.                 {
  77.                         public void actionPerformed(ActionEvent e)
  78.                         {
  79.                         d.setVisible(false);
  80.                         }
  81.                 });

  82.                 d.addWindowListener(new WindowAdapter()        //对话框本身的窗体监听器
  83.                 {
  84.                         public void windowClosing(WindowEvent e)
  85.                         {
  86.                                 d.setVisible(false);
  87.                         }
  88.                 });
  89.         }
  90. //-------------------------------显示目录函数-----------------------------------
  91.         public void showDir()
  92.         {
  93.                
  94.                 String dirPath=tf.getText(); //获取文本       
  95.                        
  96.                 File dir=new File(dirPath) ;//封装成文件对象
  97.                        
  98.                 if (dir.exists()&&dir.isDirectory()) //如果dir文件对象存在且是目录
  99.                 {
  100.                        
  101.                         ta.setText("");             //清空文本域

  102.                         String[] names=dir.list();

  103.                         for (String name:names )
  104.                         {
  105.                                 ta.append(name+"\r\n");
  106.                        
  107.                         }
  108.                 }
  109.                 else
  110.                 {

  111.                         creatDialog();//<---------------------------调用对话框的创建对象函数,这里才刚刚创建对象

  112.                         String info="您输入的:"+dirPath+"路径有误,请重输";

  113.                         lab.setText(info);

  114.                         d.setVisible(true);

  115.                 }
  116.         }
  117. //-----------------------------------------创建对话框对象函数----------------------------------
  118.         public void creatDialog()
  119.         {
  120.        
  121.                 d=new Dialog(f,"错误提示",true);

  122.                 d.setBounds(500,200,240,150);

  123.                 d.setLayout(new FlowLayout());

  124.                 lab=new Label();

  125.                 diaBut=new Button("确定");

  126.                 d.add(lab);
  127.                 d.add(diaBut);

  128.                 dialogEvent();
  129.        
  130.         }

  131. //---------------------------------------------主函数-----------------------------------------------
  132.         public static void main(String[] args)
  133.         {
  134.                 new MyWindow();
  135.         }
  136. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1 lalala

查看全部评分

回复 使用道具 举报
小冰块 发表于 2013-6-23 01:27
已经帮楼主把程序改好了,详细说明请看代码中的注释:

很详细,谢谢了
回复 使用道具 举报
楼主您好。我已将您的帖子改成已解决。如果帖子发布长时间没加分。及时联系我。以免漏分的情况发生{:soso_e100:}
回复 使用道具 举报
孙百鑫 发表于 2013-6-27 07:09
楼主您好。我已将您的帖子改成已解决。如果帖子发布长时间没加分。及时联系我。以免漏分的情况发生{:soso_e ...

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