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

© franksight 高级黑马   /  2015-2-3 17:33  /  877 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. class  MyWindowDemo
  5. {
  6.         private Frame f;
  7.         private TextField tf;
  8.         private Button but;
  9.         private TextArea ta;
  10.         private Dialog d;
  11.         private Label lab;
  12.         private Button okBut;
  13.         MyWindowDemo()
  14.         {
  15.                 init();
  16.         }
  17.         public void init()
  18.         {
  19.                 f=new Frame("my window");
  20.                 f.setBounds(300,100,600,500);
  21.                 f.setLayout(new FlowLayout());
  22.                 tf=new TextField(60);
  23.                 but=new Button("转到");
  24.                 ta=new TextArea(25,70);
  25.                 //设置d的基本信息
  26.                 d=new Dialog(f,"提示信息",true);
  27.                 d.setBounds(300,200,240,200);
  28.                 d.setLayout(new FlowLayout());
  29.                 lab=new Label();
  30.                 okBut=new Button("确定");

  31.                 //将lab,OKBut加入到d(即提示信息框)中
  32.                 d.add(lab);
  33.                 d.add(okBut);

  34.                 f.add(tf);
  35.                 f.add(but);
  36.                 f.add(ta);
  37.                 myEvent();
  38.                 f.setVisible(true);
  39.         }
  40.         private void myEvent()
  41.         {
  42.                 okBut.addActionListener(new ActionListener()
  43.                 {
  44.                         public void actionPerformed(ActionEvent e)
  45.                         {
  46.                                 d.setVisible(false);
  47.                         }
  48.                 });
  49.                 //提示信息框的'X'
  50.                 d.addWindowListener(new WindowAdapter()
  51.                 {
  52.                         public void windowClosing(WindowEvent e)
  53.                         {
  54.                                 d.setVisible(false);
  55.                         }
  56.                 });
  57.                 //如果焦点在tf中,按enter键,显示目录信息
  58.                 tf.addKeyListener(new KeyAdapter()
  59.                 {
  60.                         public void keyPressed(KeyEvent e)
  61.                         {
  62.                                 if(e.getKeyCode()==KeyEvent.VK_ENTER)
  63.                                         showDir();
  64.                         }
  65.                 });
  66.                 //点击'转到' 按钮,显示目录信息
  67.                 but.addActionListener(new ActionListener()
  68.                 {
  69.                         public void actionPerformed(ActionEvent e)
  70.                         {
  71.                                 showDir();
  72.                         }
  73.                 });
  74.                 //点击窗口的'X',则关闭退出
  75.                 f.addWindowListener(new WindowAdapter()       
  76.                 {
  77.                         public void windowClosing(WindowEvent e)
  78.                         {
  79.                                 System.exit(0);
  80.                         }
  81.                 });
  82.         }
  83.         //显示目录信息
  84.         private void showDir()
  85.         {
  86.                 String dirPath=tf.getText();//获取tf的文本信息
  87.                 File dir=new File(dirPath);
  88.                 if(dir.exists()&&dir.isDirectory())
  89.                 {
  90.                         //若dir存在且是目录,则把ta的文本内容设置为空
  91.                         ta.setText("");
  92.                         String[] names=dir.list();
  93.                         for(String name:names)
  94.                         {
  95.                                 ta.append(name+"\r\n");
  96.                         }
  97.                 }else
  98.                 {
  99.                         //否则弹出错误提示信息
  100.                         String info="您输入的信息:\""+dirPath+"\"  是错误的";
  101.                         lab.setText(info);
  102.                         d.setVisible(true);
  103.                 }
  104.         }

  105.         public static void main(String[] args)
  106.         {
  107.                 new MyWindowDemo();
  108.         }
  109. }
复制代码


0 个回复

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