日期:2014-05-20  浏览次数:20710 次

Swing窗口切换的问题
今天遇到一个问题,Swing的高手们能不能帮忙分析一下原因。在一个JFrame里面弹出一个模态Dialog A,这个Dialog里面有些控件,可以继续出发弹出新的模态Dialog B,偶尔会出现切换到该窗口的时候只显示B,底层的JFrame和A都不能显示的问题。是不是和2个模态窗口有关?该怎么解决

------解决方案--------------------
没设父窗体的问题
你应该把弹出的对话框的 onwer 设为弹出它的那个窗体
------解决方案--------------------
Dialog的构造函数. 使用传入父窗体的构造. 
不要使用默认的无参数构造.即可.
------解决方案--------------------
探讨

引用:
引用:
今天遇到一个问题,Swing的高手们能不能帮忙分析一下原因。在一个JFrame里面弹出一个模态Dialog A,这个Dialog里面有些控件,可以继续出发弹出新的模态Dialog B,偶尔会出现切换到该窗口的时候只显示B,底层的JFrame和A都不能显示的问题。是不是和2个模态窗口有关?该怎么解决

通过哪个方法设置呢?


看了一下父……

------解决方案--------------------
随便乱写了个,貌似没发现LZ你说的不显示的问题。基本解决方法上面有人说了。owner的指定。
Java code
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class DialogTest extends JFrame implements ActionListener {
    
    private JDialog d1;
    private JDialog d2;
    
    public DialogTest() {
        init();
    }

    private void init() {
        this.getContentPane().add(initPanel());
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 400);
    }

    private Component initPanel() {
        JPanel panel = new JPanel();
        JButton b = new JButton("Dialog1");
        b.addActionListener(this);
        panel.add(b);
        return panel;
    }
    
    public static void main(String[] args) {
        new DialogTest();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Dialog1")) {
            JButton b = new JButton("Dialog2");
            b.addActionListener(this);
            d1 = new JDialog(this, "Im dialog one!", true);
            d1.add(b);
            d1.setSize(200, 100);
            d1.setVisible(true);
        } else if(e.getActionCommand().equals("Dialog2")) {
            d2 = new JDialog(d1, "Im dialog two!", true);
            d2.setSize(200, 50);
            d2.setVisible(true);
        }
    }
}

------解决方案--------------------
我的平台:window server2003 x86 JDK版本:java version "1.6.0_17"
没发现你说的情况咧。
打开模式对话框后,只能通过win + d 键隐藏窗体。任务栏上点击swing窗口图标后可以正常显示回原来的状态。
------解决方案--------------------
我觉得 我上面随手写的那个例子不存在这个问题
我读了你的代码,发现那两个dialog是与其它组件同级生成的,之后只是调用setVisible做显示。dialog始终是同一份拷贝。我认为从业务上来说,应该每次都生成新的dialog吧,用完就消毁。