GUI-Dialog对话框

万物皆对象 本节内容看着很长,实际上内容很短。

 

import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PracticeUsed  {

    public static void main(String[] args)   {
        new myWindow();
        
        
        
    }
}

class myWindow
{
    private Frame f;
    private Button but;
    private TextField tf;
    private TextArea ta;
    
    private Dialog d;
    private Label lab;
    private Button okBut;
    
    
    
    myWindow()
    {
        init();
    }
    public void init()
    {
        f = new Frame("我的窗口");
        f.setBounds(300,200,600,500);
        f.setLayout(new FlowLayout());
        ta = new TextArea(25,60);
        tf = new TextField(40);
        but = new Button("我的按钮");
        
        d = new Dialog(f,"提示信息-self",true);
        lab = new Label();
        okBut = new Button("确定");
        
        d.setBounds(400,200,240,150);
        d.setLayout(new FlowLayout());
        
        d.add(lab);
        d.add(okBut);
        
        
        
        f.add(tf);
        f.add(but);
        f.add(ta);
        
        
        
        myEvent();
        
        f.setVisible(true);
    }
    
    private void myEvent()

    {
        ta.addKeyListener(new KeyAdapter()
                {
                    public void keyPressed(KeyEvent e)
                    {
                        if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                        {
                            System.exit(0);
                        }
                    }
                });
        f.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                {
                    System.exit(0);
                }
            }
        });
        tf.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                {
                    System.exit(0);
                }
            }
        });
        okBut.addKeyListener(new KeyAdapter()
                {
                    public void keyPressed(KeyEvent e)
                    {
                        d.setVisible(false);
                    }
                });    
        tf.addKeyListener(new KeyAdapter()
                {
                    public void keyPressed(KeyEvent e)
                    {
                        if(e.getKeyCode()==KeyEvent.VK_ENTER)
                        {
                            String dirPath = tf.getText();
                            File f = new File(dirPath);
                            if(f.exists()&&f.isDirectory())
                            {
                                ta.setText("");
                                String[] names = f.list();
                                for( String name : names)
                                {
                                    ta.append(name+"\r\n");
                                }
                                tf.setText("");
                            }else
                            {
                                String info = "您输入的信息"+dirPath+"是错误的,请重新输入。";
                                tf.setText("");
                                lab.setText(info);
                                d.setVisible(true);
                            }
                        }
                    }
                });
                
        okBut.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("我消失了");
                d.setVisible(false);
            }
        });
        d.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.out.println("我消失了");
                d.setVisible(false);
            }
        });
        
        
        but.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        String dirPath = tf.getText();
                        File f = new File(dirPath);
                        if(f.exists()&&f.isDirectory())
                        {
                            ta.setText("");
                            String[] names = f.list();
                            for( String name : names)
                            {
                                ta.append(name+"\r\n");
                            }
                            tf.setText("");
                        }else
                        {
                            String info = "您输入的信息"+dirPath+"是错误的,请重新输入。";
                            lab.setText(info);
                            d.setVisible(true);
                        }
                        
                    }
                });
        f.addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.out.println("我关了");
                        System.exit(0);
                    }
                });
    }

}

 

posted @ 2019-11-12 10:44  蚂蚁雅黑1010  阅读(248)  评论(0编辑  收藏  举报