1

首先,我使 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);
    }
}
4

0 回答 0