本帖最后由 高鑫 于 2012-7-13 14:58 编辑
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class MyWindowDemo
- {
- private Frame f;
- private TextField tf;
- private Button but,okBut;
- private TextArea ta;
- private Dialog d;
- private Label lab;
-
- MyWindowDemo()
- {
- init();
- }
- public void init()
- {
- f=new Frame("my window");
- f.setBounds(300,100,600,500);
- f.setLayout(new FlowLayout());
-
- tf=new TextField(60);
- but=new Button("转到");
- ta=new TextArea(25,70);
-
- d=new Dialog(f,"提示信息",true);
- d.setBounds(400,200,240,150);
- d.setLayout(new FlowLayout());
-
- lab=new Label();
- okBut=new Button("确定");
- d.add(lab);
- d.add(okBut);
-
- f.add(tf);
- f.add(but);
- f.add(ta);
-
- myEvent();
- f.setVisible(true);
- }
- private void myEvent()
- {
- d.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- d.setVisible(false);
- }
- });
- okBut.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- d.setVisible(false);
- }
- });
- tf.addKeyListener(new KeyAdapter()
- {
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode()==KeyEvent.VK_ENTER)
- showDir();
- }
- });
- but.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- showDir();
- }
- });
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- }
- private void showDir()
- {
- String dirPath=tf.getText();
- File dir=new File(dirPath);
- if(dir.exists()&&dir.isDirectory())
- {
- ta.setText("");
- String [] names=dir.list();
- for(String name:names)
- {
- ta.append(name+"\r\n");
- }
- }
- else
- {
- String info="输入信息有误"+dirPath+"请重输";
- lab.setText(info);
- d.setVisible(true);
- }
- }
- public static void main(String[]args)
- {
- new MyWindowDemo();
- }
- }
复制代码 程序哪里出问题了,为什么点窗口无法关闭程序??求指点 |
|