java Dialog

Dialog构造方法

         * Dialog(Frame owner, String title, boolean modal)

        构造一个最初不可见的 Dialog,它带有指定的所有者 Frame、标题和模式。

       

备注:Dialog的模式区别在于:

true的话对话框依附于窗体,不取消Dialog不可以操作窗体,

    false的话,不取消Dialog可以操作窗体!

package june610;

import java.awt.Button;

import java.awt.Dialog;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Label;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.File;

public class FrameDemo4 {

    public static void main(String[] args) {

        final Frame f = new Frame("我的电脑");

        f.setBounds(300, 100, 600, 500);

        f.setLayout(new FlowLayout());

        Button b = new Button("转到");

        Button okBut = new Button("确定");

        final TextField tf = new TextField(60);

        final TextArea ta = new TextArea(25, 70);

        f.add(tf);

        f.add(b);

        f.add(ta);

        f.setVisible(true);

   

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

        final Label lab = new Label();//没有给出内容,用到的时候再给出!

        d.add(lab);//label标签加到Dialog上去!

        d.setBounds(400, 200, 240, 150);

        d.setLayout(new FlowLayout());

        d.add(okBut);

       

        okBut.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e){

                d.setVisible(false);

            }

        });

       

        //只可以对鼠标有作用!

        okBut.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {

                d.setVisible(false);

            }

        });

       

        d.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                d.setVisible(false);//对话框不显示

            }

        });

       

        //窗体上的操作

        f.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            }

        });

       

        //设置键盘监听器,当输入enter键的时候实现和点击鼠标同样的功能!

        /*tf.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e) {

                if (e.getKeyCode() == 10) {

                    // buttonAction();

                    run(tf,ta,f,d,lab);

                }

                System.out.println(e.getKeyCode());

            }

        });*/

       

        //和上面被注释的代码实现的是同样的功能,也是键盘控制,不过不能设定哪个键,只有enter!

        tf.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                run(tf,ta,f,d,lab);

                // System.out.println(text);

            }

        });

       

        //给转到添加键盘和鼠标双控制

        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                run(tf,ta,f,d,lab);

                // System.out.println(text);

            }

        });

       

        b.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e){

                run(tf,ta,f,d,lab);

            }

        });

    }

   

    //封装这一方法,为了方便使用(注意这个时候传递参数太多了,尽量避免这种情况的发生!

    //在一个方法内部创建的对象只有在自己方法体里面才可以直接调用,而在外部方法必须传递参数)

    public static void run(TextField tf,TextArea ta,Frame f,Dialog d,Label lab){

       

        String dirPath = tf.getText();// 获取文本(我们想验证的是路径),接下来获取文件

        File file = new File(dirPath);// 获取文件

        if (file.exists() && file.isDirectory()) {// 判断,存在否以及是否是文件夹

            ta.setText("");// 如果符合条件的话,清空以前的数据;

            String[] names = file.list();

            for (String name : names) {

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

            }

        } else {

            //备注:应该在这里构建对话框,为了内存的优化,用到的时候才创建对象,用不到就不创建!

            String info = "您输入的信息:"+dirPath+"有误,请重新输入!";

           

            lab.setText(info);

            d.setVisible(true);

           

            /**可以这样写,但是不专业,现在弹出对话框!

             * ta.setText("");

               ta.append("对不起,请确认您输入的是路径!");

             */

        }

    }

}

猜你喜欢

转载自www.cnblogs.com/fanweisheng/p/11137621.html