服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

关于 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() 之前,否则只有窗体左上角是正对屏幕或所属组件中心,整个窗体看起来会是偏向右下角的。

扫描关注微信公众号