Modal JInternal Frames - 调整大小后,Glass Pane消失

时间:2013-02-15 18:15:12

标签: java swing

首先,我使GlassPane可见,然后我添加JInternalFrame。 GlassPane成功阻止对其下的GUI元素的访问,但是当我尝试调整JInternalFrame的大小时,GlassPane消失了,我不明白为什么。

内部框架静止不动。它看起来像是将GlassPane的setVisible方法更改为false ...如何解决?

这是一些代码。当我调整JInternalFrame的大小时,glassPane就会丢失。

import java.awt.Cursor;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import javax.swing.*;
import javax.swing.event.*;

public class Example {

    private static JFrame frame = new JFrame();
    private static JDesktopPane glassPane = new JDesktopPane();

    public static void main(String[] args) {
        // make JDesktopPane (glassPane) transparent
        glassPane.setOpaque(false);
        // add empty mouse adapter to block events to other components then JInternalFrame
        glassPane.addMouseListener(new MouseAdapter(){});
        frame.setGlassPane(glassPane);

        final JInternalFrame internal = new JInternalFrame("testFrame", true, true);
        internal.setBounds(20, 20, 240, 200);

        // add listener to hide glassPane when  intenralFrame is closed
        internal.addInternalFrameListener(new InternalFrameListener(){
            public void internalFrameOpened(InternalFrameEvent e) {}
            public void internalFrameClosing(InternalFrameEvent e) {}
            public void internalFrameClosed(InternalFrameEvent e) {
                glassPane.remove(internal);
                glassPane.setVisible(false);
                glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                try {
                    internal.setClosed(true);
                } catch (PropertyVetoException e1) {
                    e1.printStackTrace();
                }           
            }
            public void internalFrameIconified(InternalFrameEvent e) {}
            public void internalFrameDeiconified(InternalFrameEvent e) {}
            public void internalFrameActivated(InternalFrameEvent e) {}
            public void internalFrameDeactivated(InternalFrameEvent e) {}

        });

        // adding exmaple components to frame
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
        frame.getContentPane().add(new JButton("button 1"));
        // button showing modal JInternalFrame
        JButton activate = new JButton("Show Modal InternalFrame");
        activate.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                glassPane.add(internal);    
                internal.setVisible(true);
                glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                glassPane.setVisible(true);
            }       
        });
        frame.getContentPane().add(activate);

        // example menu
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("test menu");
        menu.add(new JMenuItem("test"));
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}

0 个答案:

没有答案