java图形设计,窗口阻塞的问题。
打开一个java窗口(原JFrame),里面有个按钮,点击后出现另一个窗口(新JFrame),如何使得原窗口处于阻塞的状态(即新窗口才是当前活动窗口,要想操作原窗口,就必...
打开一个java窗口(原JFrame),里面有个按钮,点击后出现另一个窗口(新JFrame),如何使得原窗口处于阻塞的状态(即新窗口才是当前活动窗口,要想操作原窗口,就必须关闭新窗口)?
展开
1个回答
展开全部
在 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();
}
}
其方法可以在构造函数(例如“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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询