在JFrame/JDesktopPane中可以看到JInternalFrame时,如何禁用/启用JFrame组件? [英] How to Disable/Enable JFrame Component when JInternalFrame isVisible inside JFrame/JDesktopPane?

查看:125
本文介绍了在JFrame/JDesktopPane中可以看到JInternalFrame时,如何禁用/启用JFrame组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JFrame/JDesktopPane内部可见JInternalFrame时如何禁用/启用JFrame组件?例如,使每个JInternalFrame1JDesktopPane(在JFrame上设置的桌面窗格)上可见,所有帧组件都在JMenuBar/JButton/etc之类.设置为禁用.

How to Disable/Enable JFrame components when JInternalFrame isVisible inside JFrame/JDesktopPane? For example, make every JInternalFrame1 visible on the JDesktopPane (desktop pane set on the JFrame) all frame components like JMenuBar/JButton/etc. set disabled.

有解决方案吗?

推荐答案

使用JXLayer

That makes use of JXLayer and JHLabs filters, basically, it creates a custom frame that wraps the JRootPane in a JXLayer, which allows you to "disable" the entire frame...

import com.jhlabs.image.BlurFilter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.plaf.effect.BufferedImageOpEffect;
import org.jdesktop.jxlayer.plaf.ext.LockableUI;

public class EnabledFrame {

    public static void main(String[] args) {
        new EnabledFrame();
    }

    public EnabledFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final LayeredFrame frame = new LayeredFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                JMenuBar mb = new JMenuBar();
                JMenu mFile = new JMenu("File");
                mFile.add("Exit");
                frame.setJMenuBar(mb);
                mb.add(mFile);

                JButton btn = new JButton("Click me");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.setEnabled(false);
                    }
                });

                frame.add(btn);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class LayeredFrame extends JFrame {

        private BlurableLockedUI lockUI;
        private JXLayer<JRootPane> rootLayer;

        public LayeredFrame(String title) throws HeadlessException {
            super(title);
        }

        @Override
        protected void setRootPane(JRootPane root) {
            if (rootLayer != null) {
                remove(rootLayer);
            }
            super.setRootPane(root);
            JRootPane rootPane = getRootPane();
            rootLayer = new JXLayer<>(root);
            rootLayer.setUI(getLockUI());
            add(rootLayer, BorderLayout.CENTER);
        }

        protected BlurableLockedUI getLockUI() {
            if (lockUI == null) {
                lockUI = new BlurableLockedUI();
            }
            return lockUI;
        }

        @Override
        public void setEnabled(boolean b) {
            getLockUI().setLocked(!b);
            super.setEnabled(b);
        }

    }

    public class BlurableLockedUI extends LockableUI {

        public BlurableLockedUI() {
            super(new BufferedImageOpEffect(new BlurFilter()));
        }

        public void repaint() {
            setDirty(true);
        }

        public void invalidate() {
            setDirty(true);
        }

        public void revalidate() {
            setDirty(true);
        }

    }

}

这篇关于在JFrame/JDesktopPane中可以看到JInternalFrame时,如何禁用/启用JFrame组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆