打开另一个窗口时将JFrame变为黑色

Han*_*ken 5 java user-interface swing awt jframe

当焦点在另一个窗口时,我不想让我的主JFrame变暗.这是来自足球经理2012游戏的一个例子.首先选择主窗口,它看起来应该是,然后当它加载时,它变得更暗和不可选.我不想在我自己的应用程序上有这个效果,但我不确定如何,甚至不确定谷歌的什么?

我猜它出现了一个JWindow,JFram在后台变得无法选择.我正计划在我的应用程序的帮助窗口中使用它,现在就是JWindow.

在此输入图像描述 在此输入图像描述

Ing*_*gel 8

安德鲁汤普森有正确的想法,只有它更容易使用你的框架的玻璃窗格功能JRootPane.这是一些有效的代码:在你的框架类中,调用

getRootPane().setGlassPane(new JComponent() {
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0, 0, 0, 100));
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
});
Run Code Online (Sandbox Code Playgroud)

然后,要显示"幕布",请调用

getRootPane().getGlassPane().setVisible(true);
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,将Alpha透明度值更改为100,以便找到合适的暗度.


..新窗口关闭后,JWrame恢复正常.我尝试了setVisible(false),但它没有用.

它适用于此示例.

阴影框架

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ShadowedFrame extends JFrame {

    ShadowedFrame() {
        super("Shadowed Frame");
        getRootPane().setGlassPane(new JComponent() {
            public void paintComponent(Graphics g) {
                g.setColor(new Color(0, 0, 0, 100));
                g.fillRect(0, 0, getWidth(), getHeight());
                super.paintComponent(g);
            }
        });
        JButton popDialog = new JButton("Block Frame");
        popDialog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                getRootPane().getGlassPane().setVisible(true);
                JOptionPane.showMessageDialog(ShadowedFrame.this, "Shady!");
                getRootPane().getGlassPane().setVisible(false);
            }
        });
        setContentPane(popDialog);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setSize(350,180);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ShadowedFrame().setVisible(true);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)