Java对话框(Dialog)实例

Dialog 是一个带标题和边界的顶层窗口,边界一般用于从用户处获得某种形式的输入。 Dialog 的大小包括边界所指定的任何区域。边界区的维度可以使用 getInsets 方法获得,但是,由于这些维度是依赖于平台的,因此只有通过调用 pack 或 show 将 dialog 设置为可显示的,才能获得有效的 insets 值。

Dialog 是一个带标题和边界的顶层窗口,边界一般用于从用户处获得某种形式的输入。 Dialog 的大小包括边界所指定的任何区域。边界区的维度可以使用 getInsets 方法获得,但是,由于这些维度是依赖于平台的,因此只有通过调用 packshow 将 dialog 设置为可显示的,才能获得有效的 insets 值。由于 dialog 的总大小包括了边界区,因此边界有效地模糊了 dialog 的部分区域,约束了可用于在矩形中呈现或显示子部件的区域,矩形左上角的位置为 (insets.left, insets.top),宽度为 width - (insets.left + insets.right),长度为 height - (insets.top + insets.bottom)

Dialog 的默认布局为 BorderLayout

Dialog 可以使用 setUndecorated 关闭本机装饰(例如 Frame & Titlebar)。只有在 dialog 不是 displayable 时才能完成此操作。

Dialog 在构造时必须有一个框架,或者将另一个 dialog 定义为它的所有者。当可见的 dialog 的所有者窗口被最小化时,dialog 会自动隐藏为对用户不可见。当所有者窗口被还原时,dialog 重新又变为用户可见的。

dialog 可以是无模式的(默认情况下)或有模式的。一个有模式的 dialog 将阻断输入到应用程序中其他所有顶层窗口的内容(将此 dialog 作为其所有者创建的窗口除外)。

Dialog 能够产生以下 WindowEventsWindowOpenedWindowClosingWindowClosedWindowActivatedWindowDeactivatedWindowGainedFocusWindowLostFocus。  

java广告位

实例:

import java.awt.*;
import java.awt.event.*;

public class TestDialog implements ActionListener{
    private Label info;
    private Dialog loginDialog;
    private Dialog quitDialog;
    private TextField tf_name;
    private TextField tf_psw;
    
    public static void main( String args[]) {
        new TestDialog().init();
    }

    public void init(){
        Frame f = new Frame("注册窗口");
        Button login = new Button("登录");
        Button regist = new Button("注册");
        Button help = new Button("帮助");
        Button exit = new Button("退出");
        Panel p = new Panel();
        p.setLayout(new GridLayout(1,4));
        p.add(login);
        p.add(regist);
        p.add(help);
        p.add(exit);
        info = new Label("您尚未登录");
        f.add(p,"North");
        f.add(info,"Center");
        
        login.addActionListener(this);
        exit.addActionListener(this);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                TestDialog.this.quit();
            }
        });  
        f.setSize(500,350);
        f.setLocation(450,200);
        f.setVisible( true);
        loginDialog = this.createLoginDialog(f);
        quitDialog = this.createQuitDialog(f);
    }
    
    public Dialog createLoginDialog(Frame f){
        Dialog d = new Dialog(f,"登录对话框",true);
        Label note = new Label("请输入注册信息");
        
        Panel pa = new Panel();
        pa.setLayout(new GridLayout(2,1));
        pa.add(new Label("用户名:"));
        pa.add(new Label("密  码:"));
        
        Panel pc = new Panel();
        pc.setLayout(new GridLayout(2,1));
        tf_name = new TextField();
        tf_psw = new TextField(); 
        tf_psw.setEchoChar('*');
        pc.add(tf_name);
        pc.add(tf_psw);
        
        Panel pb = new Panel();
        pb.setLayout(new GridLayout(1,2));
        Button submit = new Button("提交");
        Button cancel = new Button("取消");
        submit.setActionCommand("submitLogin");
        cancel.setActionCommand("cancelLogin");
        submit.addActionListener(this);
        cancel.addActionListener(this);
        pb.add(submit);
        pb.add(cancel);
        
        d.add(note,"North");
        d.add(pa,"West");
        d.add(pc,"Center");
        d.add(pb,"South");
        d.setSize(160,120);
        d.setLocation(400,200);
        return d;
    }
    
    public Dialog createQuitDialog(Frame f){
        Dialog d = new Dialog(f,"确认退出对话框",true);
        Label note = new Label("您确定要退出程序吗?");
        Panel p = new Panel();
        //p.setLayout(new GridLayout(1,2));
        Button confirm = new Button("确定");
        Button cancel = new Button("取消");
        confirm.setActionCommand("confirmQuit");
        cancel.setActionCommand("cancelQuit");
        confirm.addActionListener(this);
        cancel.addActionListener(this);
        p.add(confirm);
        p.add(cancel);
        d.setSize(160,120);
        d.setLocation(400,200);
        d.add(note,"Center");
        d.add(p,"South");
        return d;
    }
    
    public void actionPerformed(ActionEvent e){
        String s = e.getActionCommand();
        if(s.equals("登录")){
            loginDialog.setVisible(true);
        }else if(s.equals("退出")){
            this.quit();
        }else if(s.equals("confirmQuit")){
            System.exit(0);
        }else if(s.equals("cancelQuit")){
            quitDialog.setVisible(false);
        }else if(s.equals("submitLogin")){
            String name = tf_name.getText();
            String password = tf_psw.getText();
            if(name.equals("Scott") && password.equals("Tiger")){
                info.setText("欢迎您: " + name + " 用户");
            }else{
                info.setText("验证失败,错误的用户名/密码!");
            }
            loginDialog.setVisible(false);
        }else if(s.equals("cancelLogin")){
            loginDialog.setVisible(false);
        }
    }
    
    public void quit(){
        quitDialog.setVisible(true);
    }
}

效果图:

Java对话框(dialog)效果图

人生就像赛跑,不在乎你是否第一个到达终点,而在乎你有没有跑完全程。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号