哪位帮忙给看一下下面的程序,我想在模态和非模态下实现相同的功能,既点子窗体的“退出”按钮后,主窗体仍然可用。我现在的问题是当为模态时,点子窗体退出按钮后,主窗体仍然不可用,请给实现一下:////主文件 Test.java
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 pane1 = new JPanel();
JPanel pane2 = new JPanel();
b = new JCheckBox("模态窗口");
pane1.add(b);
button = new JButton("弹出窗体");
pane2.add(button);
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(320,200);
my_f.setLocationRelativeTo(null);
my_f.setVisible(true);
}
});
frame.getContentPane().add(pane1, BorderLayout.NORTH);
frame.getContentPane().add(pane2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 450);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
} //////子文件 My_Frame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
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(); setLayout(new FlowLayout(1));
JButton btb=new JButton("退出");
btb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
My_Frame.this.dispose(); //退出子窗体
}
});
add(btb);
setVisible(true);
}
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();
}
}
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 pane1 = new JPanel();
JPanel pane2 = new JPanel();
b = new JCheckBox("模态窗口");
pane1.add(b);
button = new JButton("弹出窗体");
pane2.add(button);
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(320,200);
my_f.setLocationRelativeTo(null);
my_f.setVisible(true);
}
});
frame.getContentPane().add(pane1, BorderLayout.NORTH);
frame.getContentPane().add(pane2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 450);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
} //////子文件 My_Frame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
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(); setLayout(new FlowLayout(1));
JButton btb=new JButton("退出");
btb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
My_Frame.this.dispose(); //退出子窗体
}
});
add(btb);
setVisible(true);
}
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();
}
}
再setEnabled(true),而你只是dispose了子窗体,并没有让父窗体可用。如果你真的要用模态,还是建议你用JDialog的子类构造模态