频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Swing 中设置模态窗体和启动位置

来源:CSDN.NET 作者: 出处:巧巧读书 2007-04-07 进入讨论组

关于 Modal 窗体

    在 Swing 中只有 JDialog 可以设置为 Modal 窗体,其方法可以在构造函数(例如“JDialog(Frame owner, boolean modal)”)中传参数,也可以用 setModal(boolean b) 方法设定,

这个方法是从 Dialog 类继承的。

    在 JFrame 类中,无法通过如 JDialog 的方法设置 Modal 窗体,在 CSDN 有朋友尝试通过在 windowDeiconified() 时 requestFocus() 来模拟 Modal 窗体,代码如下:


Swing 中设置模态窗体和启动位置(图一)Swing 中设置模态窗体和启动位置(图二)public class MyModalFrame extends JFrame implements WindowListener ...{
Swing 中设置模态窗体和启动位置(图三)    private JFrame frame = null;
Swing 中设置模态窗体和启动位置(图三)    private boolean modal = false;
Swing 中设置模态窗体和启动位置(图三)    private String title = null;
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public MyModalFrame() ...{
Swing 中设置模态窗体和启动位置(图三)        this(null, false);
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public MyModalFrame(JFrame frame) ...{
Swing 中设置模态窗体和启动位置(图三)        this(frame, false);
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public MyModalFrame(JFrame frame, boolean modal) ...{
Swing 中设置模态窗体和启动位置(图三)        this(frame, modal, "");
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public MyModalFrame(JFrame frame, boolean modal, String title) ...{
Swing 中设置模态窗体和启动位置(图三)        super(title);
Swing 中设置模态窗体和启动位置(图三)        this.frame = frame;
Swing 中设置模态窗体和启动位置(图三)        this.modal = modal;
Swing 中设置模态窗体和启动位置(图三)        this.title = title;
Swing 中设置模态窗体和启动位置(图三)        this.init();
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    private void init() ...{
Swing 中设置模态窗体和启动位置(图三)        if(modal)
Swing 中设置模态窗体和启动位置(图三)            frame.setEnabled(false);
Swing 中设置模态窗体和启动位置(图三)        this.addWindowListener(this);
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowOpened(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowClosing(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图三)        if(modal)
Swing 中设置模态窗体和启动位置(图三)            frame.setEnabled(true);
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowClosed(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowIconified(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowDeiconified(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowActivated(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图三)
Swing 中设置模态窗体和启动位置(图四)Swing 中设置模态窗体和启动位置(图五)    public void windowDeactivated(WindowEvent windowEvent) ...{
Swing 中设置模态窗体和启动位置(图三)        if(modal)
Swing 中设置模态窗体和启动位置(图三)            this.requestFocus();
Swing 中设置模态窗体和启动位置(图六)    }
Swing 中设置模态窗体和启动位置(图七)}

关于窗体启动位置

    有时候想要让窗体启动后在屏幕中间启动,有种比较复杂的方法:

Swing 中设置模态窗体和启动位置(图八)Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Swing 中设置模态窗体和启动位置(图八)Dimension size = frame.getSize();
Swing 中设置模态窗体和启动位置(图八)int x = (screenSize.width - size.width) / 2;
Swing 中设置模态窗体和启动位置(图八)int y = (screenSize.height - size.height) / 2;
Swing 中设置模态窗体和启动位置(图八)frame.setLocation( x, y );

    在 Java 1.4 版之后可以用一条语句代替:

Swing 中设置模态窗体和启动位置(图八)frame.setLocationRelativeTo(null);


    Java API 文档中对此方法描述如下:public void setLocationRelativeTo(Component c)
设置此窗口相对于指定组件的位置。如果此组件当前未显示,或者 c 为 null,则此窗口位于屏幕的中央。如果该组件的底部在视线以外,则将该窗口放置在 Component 最接近窗口中心的一侧。因此,如果 Component 在屏幕的右部,则 Window 将被放置在左部,反之亦然。

    在应用此方法时应该注意的一点是,setSize() 方法一定要放在 setLocationRelativeTo() 之前,否则只有窗体左上角是正对屏幕或所属组件中心,整个窗体看起来会是偏向右下角的。

文字:http://www.qqread.com/java/2007/04/f307333.html进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章