您的位置:首页 > 产品设计 > UI/UE

GUI学习笔记MyWindowDemo.java

2011-12-02 23:45 288 查看
 

import java.awt.*;

import java.awt.event.*;

import java.io.*;//还不会的知识

class MyWindowDemo //extends Frame

{

 private Frame f;

 private Button but;

 private TextField tf;

 private TextArea ta;

 private Dialog d;

 private Label lab;

 private Button okbut;

 MyWindowDemo()

 {

  init();

 }

 public void init()

 {

  f=new Frame("my window");

  f.setBounds(200,100,600,550);

  f.setLayout(new FlowLayout());

  tf=new TextField(60);

  f.add(tf);

  but=new Button("转到");

  f.add(but);

  ta=new TextArea(15,70);

  f.add(ta);

  d=new Dialog(f,"提示信息-self",true);

  d.setBounds(215,115,200,100);

  d.setLayout(new FlowLayout());

  lab=new Label();

  d.add(lab);

  okbut=new Button("确定");

  d.add(okbut);

  myEvent();

  f.setVisible(true);

 }

 public void myEvent()

 {

  f.addWindowListener(new WindowAdapter()

  {

   public void windowClosing(WindowEvent e)

   {

    System.exit(0);

   }

  }

  );

  okbut.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    d.setVisible(false);

   }

  }

  );

  d.addWindowListener(new WindowAdapter()

  {

   public void windowClosing(WindowEvent e)

   {

    d.setVisible(false);

   }

  }

  );

  but.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    

    showDir();

    //ta.setText("你错误了");

    /*

    String text=tf.getText();

    ta.setText(text);//System.out.println(text);

    tf.setText("");

    */

   }

  }

  );

  tf.addKeyListener(new KeyAdapter()

  {

   public void keyPressed(KeyEvent e)

   {

    if(e.getKeyCode()==KeyEvent.VK_ENTER)

    {

     showDir();

    }

   }

  }

  );

  okbut.addKeyListener(new KeyAdapter()

  {

   public void keyPressed(KeyEvent e)

   {

    if(e.getKeyCode()==KeyEvent.VK_ENTER)

     d.setVisible(false);

   }

  }

  );

 }

 public void showDir()

 {

  String dirPath=tf.getText();//还不会的知识

  File dir=new File(dirPath);

  if(dir.exists() && dir.isDirectory())

  {

   String[] names=dir.list();

   for(String name : names)

   {

    tf.setText("");

    ta.append(name+"\r\n");

   }

  }

  else

  {

   String info="你输入信息:"+dirPath+"是错误的。请重输!";

   lab.setText(info);

   d.setVisible(true);

  }

 }

 /*class ActionDemo implements ActionListener

 {

 }

 */

 public static void main(String[] args)

 {

  new MyWindowDemo();

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息