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

© ifuzhen 中级黑马   /  2014-5-3 15:54  /  974 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;

  4. class  MyWindowDemo
  5. {
  6.        
  7.         private Frame f;
  8.         private TextField tf;
  9.         private Button btn;
  10.         private TextArea ta;
  11.         private Dialog d;
  12.         private Label lab;
  13.         private Button okBtn;

  14.         MyWindowDemo()
  15.         {
  16.                 init();
  17.         }

  18.         public void init()
  19.         {
  20.                 f=new Frame("my window");
  21.                 f.setBounds(300,300,500,300);
  22.                 f.setLayout(new FlowLayout());

  23.                 tf =new TextField(60);
  24.                 btn =new Button("go");
  25.                 ta =new TextArea(60,60);
  26.                
  27.                 d =new Dialog(f,"my-提示对话框",true);
  28.                 d.setBounds(400,300,200,150);
  29.                 d.setLayout(new FlowLayout());

  30.                 lab=new Label("",Label.RIGHT);
  31.                 d.add(lab);
  32.                 okBtn=new Button("确定");
  33.                 d.add(okBtn);

  34.                 f.add(tf);
  35.                 f.add(btn);
  36.                 f.add(ta);
  37.                

  38.                  

  39.                 myEvent();

  40.                 f.setVisible(true);

  41.         }

  42.         private void myEvent()
  43.         {
  44.                 btn.addActionListener(new ActionListener()
  45.                 {
  46.                         public void actionPerformed(ActionEvent e)
  47.                         {
  48.                                
  49.                                 showDir();
  50.                         }
  51.                 });
  52.                

  53.                 //添加按键盘的回车键,注意现在事件源为文本框
  54.                 tf.addKeyListener(new KeyAdapter()
  55.                 {
  56.                         public void keyPressed(KeyEvent e)//注意方法名写错是不会报错的
  57.                         {
  58.                                
  59.                                 if (e.getKeyCode()==KeyEvent.VK_ENTER)
  60.                                 {
  61.                                         showDir();
  62.                                 }
  63.                                
  64.                         }
  65.                 });


  66.                 f.addWindowListener(new WindowAdapter()
  67.                 {
  68.                         public void windowClosing(WindowEvent e)
  69.                         {
  70.                                 System.exit(0);
  71.                                 sop("fram-window exit");
  72.                         }
  73.                 });


  74.                 d.addWindowListener(new WindowAdapter()
  75.                 {
  76.                         public void windowClosing(WindowEvent e)
  77.                         {
  78.                                 d.setVisible(false);
  79.                         }
  80.                 });
  81.                
  82.                 okBtn.addActionListener(new ActionListener()
  83.                 {
  84.                         public void actionPerformed(ActionEvent e)
  85.                         {
  86.                                 d.setVisible(false);
  87.                         }
  88.                 });
  89.                
  90.                 //为何不起作用呢?
  91.                 d.addKeyListener(new KeyAdapter()
  92.                 {
  93.                         public void KeyPressed(KeyEvent e)
  94.                         {
  95.                                 if (e.getKeyCode()==KeyEvent.VK_ENTER)
  96.                                 {
  97.                                         d.setVisible(false);
  98.                                 }
  99.                         }
  100.                 });

  101.         }

  102.         public void showDir()
  103.         {
  104.                 String dirPath=tf.getText();

  105.                 File dir =new File(dirPath);
  106.                 if (dir.exists() && dir.isDirectory())
  107.                 {
  108.                         ta.setText("");
  109.                         String[] names =dir.list();
  110.                         for (String name : names)
  111.                         {
  112.                                 ta.append(name+"\r\n");
  113.                         }
  114.                 }
  115.                 else
  116.                 {
  117.                         String info="找不到目录:"+dir+",请重新输入";
  118.                         lab.setText(info);
  119.                         d.setVisible(true);
  120.                 }
  121.         }
  122.         public static void main(String[] args)
  123.         {
  124.                 new MyWindowDemo();
  125.         }

  126.         public static void sop(Object obj)
  127.         {
  128.                 System.out.println(obj);
  129.         }
  130. }
复制代码


想要的效果就是当弹出一个带有"确定按钮"的对话框时,当回车时对话框会消失,可是给对话框添加了键盘监听事件,好像不起作用呢?

QQ截图20140503155335.png (10.49 KB, 下载次数: 28)

对话框

对话框

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马