Swing中,如何设置JFrame的modal模式?

scottmx 2006-02-22 07:13:39
在Swing中,从一个JFrame打开另一个JFrame,如何设置新JFrame(子窗口)为Modal(即所有关于此程序的操作只能在子JFrame中进行,不能单击父窗口。我知道JDialog可以,但要对JFrame如此操作,要如何呢?
谢谢!
...全文
859 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
scottmx 2006-03-08
  • 打赏
  • 举报
回复
不错,不错
scottmx 2006-02-23
  • 打赏
  • 举报
回复
关键是我不想隐藏父窗口啊,想达到Eclipse那种当Eclipse主程序在运行时,从菜单选择Preference时,Preference窗口可以操作,但Eclipse主界面不能操作(单击)那种结果。
dudee 2006-02-23
  • 打赏
  • 举报
回复
好象不能设.你可以把父JFrame隐藏啊,当退出子JFrame时再把父JFrame显示.
mq612 2006-02-23
  • 打赏
  • 举报
回复
我在表面层模拟了一个,挺有意思的,你玩玩

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test {
private JFrame frame = null;
private JButton button = null;
private JCheckBox b = null;

public Test() {
frame = new JFrame("测试窗体");
JPanel pane = new JPanel();
b = new JCheckBox("模态窗口");
pane.add(b);
button = new JButton("弹出窗体");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
My_Frame my_f = new My_Frame(frame, b.isSelected(), String.valueOf(b.isSelected()));
my_f.setSize(120, 100);
my_f.setLocationRelativeTo(null);
my_f.setVisible(true);
}
});
frame.getContentPane().add(button);
frame.getContentPane().add(pane, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
new Test();
}
}

/************************* 下面是拓展的JFrame ***************************/

import javax.swing.*;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;

public class My_Frame
extends JFrame implements WindowListener{
private JFrame frame = null;
private boolean modal = false;
private String title = null;
public My_Frame() {
this(null, false);
}
public My_Frame(JFrame frame){
this(frame, false);
}
public My_Frame(JFrame frame, boolean modal) {
this(frame, modal, "");
}
public My_Frame(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();
}
}
mq612 2006-02-23
  • 打赏
  • 举报
回复
JFrame不具备模态窗口的条件

Window类的构造:
Window(GraphicsConfiguration gc){} //Frame和JFrame采用了这个构造方法
//下面这三个构造方法才能拓展出模态窗口,因为它们指定了一个Frame或Window作为其所有者
Window(Frame owner){}
Window(Window owner){}
Window(Window owner, GraphicsConfiguration gc){}



Dialog继承的是Window类,它的构造如下:
public Dialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
super(owner, gc); //重点在这里,所以它可以设置模态
this.title = title;
this.modal = modal;
setFocusTraversalPolicy(KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalPolicy());
}

JFrame和它的父类Frame的构造:
public Frame(String title, GraphicsConfiguration gc) {
super(gc); //采用了Window的第一种构造
init(title, gc);
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧