Swing 中设置模态窗体和启动位置
来源:互联网 发布:java reactor模式 编辑:程序博客网 时间:2024/05/18 10:11
关于 Modal 窗体
在 Swing 中只有 JDialog 可以设置为 Modal 窗体,其方法可以在构造函数(例如“JDialog(Frame owner, boolean modal)”)中传参数,也可以用 setModal(boolean b) 方法设定,这个方法是从 Dialog 类继承的。
在 JFrame 类中,无法通过如 JDialog 的方法设置 Modal 窗体,在 CSDN 有朋友尝试通过在 windowDeiconified() 时 requestFocus() 来模拟 Modal 窗体,代码如下:
关于窗体启动位置
有时候想要让窗体启动后在屏幕中间启动,有种比较复杂的方法:
在 Java 1.4 版之后可以用一条语句代替:
Java API 文档中对此方法描述如下:public void setLocationRelativeTo(Component c)
设置此窗口相对于指定组件的位置。如果此组件当前未显示,或者 c 为 null,则此窗口位于屏幕的中央。如果该组件的底部在视线以外,则将该窗口放置在 Component 最接近窗口中心的一侧。因此,如果 Component 在屏幕的右部,则 Window 将被放置在左部,反之亦然。
在应用此方法时应该注意的一点是,setSize() 方法一定要放在 setLocationRelativeTo() 之前,否则只有窗体左上角是正对屏幕或所属组件中心,整个窗体看起来会是偏向右下角的。
在 Swing 中只有 JDialog 可以设置为 Modal 窗体,其方法可以在构造函数(例如“JDialog(Frame owner, boolean modal)”)中传参数,也可以用 setModal(boolean b) 方法设定,这个方法是从 Dialog 类继承的。
在 JFrame 类中,无法通过如 JDialog 的方法设置 Modal 窗体,在 CSDN 有朋友尝试通过在 windowDeiconified() 时 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 中设置模态窗体和启动位置
- Swing 中设置模态窗体和启动位置
- Swing 中设置模态窗体和启动位置
- Swing中窗体启动位置
- 设置java窗体启动位置
- Swing设置窗体最大化
- c# 窗体位置设置
- winform窗体位置设置
- c# 设置窗体位置
- java-swing-设置窗体大小
- java swing中添加菜单和 选项卡窗体 。
- java swing中添加菜单和选项卡窗体
- java swing中添加菜单和选项卡窗体
- 一个窗体中Process启动另一个窗体,设置当前窗体为非顶层窗体
- jqgrid 设置 编辑窗体 的位置和大小的方法
- 使用WindowManager.LayoutParams的x和y设置窗体位置
- Swing 透明窗体和不规则窗体
- 用 Eclipse 进行可视化 Java 界面设计
- 创建 SWT 项目的原生库导入问题
- 出圈题目
- 一个证明表达式的求值顺序只是部分由操作符的优先级决定的程序
- 整理一下最近的RIA行业新闻吧
- Swing 中设置模态窗体和启动位置
- 连接字符串大全
- 一个合格的程序员该做的事情
- [zt]我国科学在自主创新方面为何举步维艰(邹承鲁)
- 看“华为人”如何管理时间
- Ajax学习 网址备忘录
- 使用Ajax时的十个常犯的错误
- MyEclipse 5.0 + WebLogic 9.2 配置详解
- MyEclipse + WebLogic 开发 EJB 需注意