java 下的模态窗体

收集自 http://topic.csdn.net/t/20060222/19/4571358.html

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);
}

我在表面层模拟了一个,挺有意思的,你玩玩
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();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值