概述
关于 Modal 窗体
在 Swing 中只有 JDialog 可以设置为 Modal 窗体,其方法可以在构造函数(例如“JDialog(Frame owner, boolean modal)”)中传参数,也可以用 setModal(boolean b) 方法设定,这个方法是从 Dialog 类继承的。
在 JFrame 类中,无法通过如 JDialog 的方法设置 Modal 窗体,在 CSDN 有朋友尝试通过在 windowDeactivated() 时 requestFocus() 来模拟 Modal 窗体,代码如下:
public
class
MyModalFrame
extends
JFrame
implements
WindowListener
{
private JFrame frame = null;
private boolean modal = false;
private String title = null;
public MyModalFrame() {
this(null, false);
}
public MyModalFrame(JFrame frame) {
this(frame, false);
}
public MyModalFrame(JFrame frame, boolean modal) {
this(frame, modal, "");
}
public MyModalFrame(JFrame frame, boolean modal, String title) {
super(title);
this.frame = frame;
this.modal = modal;
this.title = title;
this.init();
}
private void init() {
if(modal)
frame.setEnabled(false);
this.addWindowListener(this);
}
public void windowOpened(WindowEvent windowEvent) {
}
public void windowClosing(WindowEvent windowEvent) {
if(modal)
frame.setEnabled(true);
}
public void windowClosed(WindowEvent windowEvent) {
}
public void windowIconified(WindowEvent windowEvent) {
}
public void windowDeiconified(WindowEvent windowEvent) {
}
public void windowActivated(WindowEvent windowEvent) {
}
public void windowDeactivated(WindowEvent windowEvent) {
if(modal)
this.requestFocus();
}
}
private JFrame frame = null;
private boolean modal = false;
private String title = null;
public MyModalFrame() {
this(null, false);
}
public MyModalFrame(JFrame frame) {
this(frame, false);
}
public MyModalFrame(JFrame frame, boolean modal) {
this(frame, modal, "");
}
public MyModalFrame(JFrame frame, boolean modal, String title) {
super(title);
this.frame = frame;
this.modal = modal;
this.title = title;
this.init();
}
private void init() {
if(modal)
frame.setEnabled(false);
this.addWindowListener(this);
}
public void windowOpened(WindowEvent windowEvent) {
}
public void windowClosing(WindowEvent windowEvent) {
if(modal)
frame.setEnabled(true);
}
public void windowClosed(WindowEvent windowEvent) {
}
public void windowIconified(WindowEvent windowEvent) {
}
public void windowDeiconified(WindowEvent windowEvent) {
}
public void windowActivated(WindowEvent windowEvent) {
}
public void windowDeactivated(WindowEvent windowEvent) {
if(modal)
this.requestFocus();
}
}
关于窗体启动位置
有时候想要让窗体启动后在屏幕中间启动,有种比较复杂的方法:
Dimension screenSize
=
Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = frame.getSize();
int x = (screenSize.width - size.width) / 2 ;
int y = (screenSize.height - size.height) / 2 ;
frame.setLocation( x, y );
Dimension size = frame.getSize();
int x = (screenSize.width - size.width) / 2 ;
int y = (screenSize.height - size.height) / 2 ;
frame.setLocation( x, y );
在 Java 1.4 版之后可以用一条语句代替:
frame.setLocationRelativeTo(null);
Java API 文档中对此方法描述如下:public void setLocationRelativeTo(Component c)
设置此窗口相对于指定组件的位置。如果此组件当前未显示,或者 c 为 null,则此窗口位于屏幕的中央。如果该组件的底部在视线以外,则将该窗口放置在 Component 最接近窗口中心的一侧。因此,如果 Component 在屏幕的右部,则 Window 将被放置在左部,反之亦然。
在应用此方法时应该注意的一点是,setSize() 方法一定要放在 setLocationRelativeTo() 之前,否则只有窗体左上角是正对屏幕或所属组件中心,整个窗体看起来会是偏向右下角的。
最后
以上就是阔达小蘑菇为你收集整理的Swing 中设置模态窗体和启动位置 的全部内容,希望文章能够帮你解决Swing 中设置模态窗体和启动位置 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复