Swing 中设置模态窗体和启动位置
来源:互联网 发布:营销网络的建立报告 编辑:程序博客网 时间:2024/06/06 01:05
关于 Modal 窗体
在 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 透明窗体和不规则窗体
- 节点包含checkbox的Swing树控件
- Symbian C++开发环境配置
- 调用javacomm20-win32API注意事项
- 响应CSliderCtrl和cscrollbar拖动消息
- 用WINRAR制作Windows Mobile软件PC端安装包
- Swing 中设置模态窗体和启动位置
- 电源管理
- 简单几招 就能让操作系统不再假死机
- 计划任务工具 cron 的配置和说明
- 如何调试今日插件,tray Icon,以及DLL。
- Sharing a Single Style with Heterogeneous Elements
- Spring MVC 入门
- 一个简单的spring mvc实例
- 准备书籍名称收藏