社区
Java SE
帖子详情
Swing中,如何设置JFrame的modal模式?
scottmx
2006-02-22 07:13:39
在Swing中,从一个JFrame打开另一个JFrame,如何设置新JFrame(子窗口)为Modal(即所有关于此程序的操作只能在子JFrame中进行,不能单击父窗口。我知道JDialog可以,但要对JFrame如此操作,要如何呢?
谢谢!
...全文
859
5
打赏
收藏
Swing中,如何设置JFrame的modal模式?
在Swing中,从一个JFrame打开另一个JFrame,如何设置新JFrame(子窗口)为Modal(即所有关于此程序的操作只能在子JFrame中进行,不能单击父窗口。我知道JDialog可以,但要对JFrame如此操作,要如何呢? 谢谢!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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);
}
Swing
-
JFrame
浅谈如何在
JFrame
中
添加背景色及背景图片
JFrame
默认是BorderLayout JPanel默认是FlowLayout。 1.
JFrame
设置
背景色,注意体会注释的那句话。 package com.tools; import java.awt.Color; import javax.
swing
.
JFrame
; public class Test extends
JFrame
{ public stati
swing
中
如何实现
JFrame
为模态窗口
在
Swing
中
,当连个画面都是继承
JFrame
的时候,如何将将其
中
一个画面设为另一个画面的模态窗口呢?本人总结了一下个人的经验,以供交流。 父画面: 父画面在打开子画面的时候,setEnabled(false)同时将自己的引用传给模态画面; 模
java
中
mo
dal
,Java
swing
中
的
Mo
dal
Window顶部的
Mo
dal
Window?
I have the following problem:I have a main application window (
JFrame
) that fills the whole screen.When a button is clicked, a smaller window appears, to let the user input some Data. While the User d...
java新建
jframe
_如何在
Swing
java
中
创建
JFrame
模型
回答(11)2 years ago下面是一些示例代码,它将在 JDialog
中
显示 JPanel panel ,它是 Frame parentFrame 的模态 . 除了构造函数之外,它遵循与打开
JFrame
相同的
模式
.final JDialog frame = new JDialog(parentFrame, frameTitle, true);frame.getContentPane(...
java创建模态窗口_如何在
Swing
中
创建模态对话框?
下面的示例展示了如何在基于
swing
的应用程序
中
创建模态对话框。使用以下API -JDialog() - 创建标准对话框。JDialog.getContentPane() - 获取对话框的内容面板。Dialog.
Mo
dal
ityType.DOCUMENT_
MO
DAL
- 将对话框显示为
模式
对话框。示例package com.yiibai.
swing
de
mo
;import java.awt.Bo...
Java SE
62,614
社区成员
307,326
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章