java - 打开另一个窗口时将 JFrame 变黑

标签 java swing user-interface awt jframe

当焦点位于另一个窗口时,我不想让我的主 JFrame 变暗。 这是游戏 Football Manager 2012 中的示例。首先选择了主窗口,看起来应该如此,然后在加载时,它变得更暗且无法选择。我不想对我自己的应用程序产生这种影响,但我不太确定如何,甚至不确定要用谷歌搜索什么?

我猜是出现了一个 JWindow 并且 JFram 在后台变得不可选择。我打算在我的应用程序的帮助窗口上使用它,现在是 JWindow。

enter image description here enter image description here

最佳答案

Andrew Thompson 的想法是正确的,只是使用框架的 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);
    }
});

然后,要显示“窗帘”,调用

getRootPane().getGlassPane().setVisible(true);

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


..wan't the JFrame to go back to normal after the new window is closed. I tried setVisible(false) but it didn't work.

它在这个例子中有效。

Shadowed Frame

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);
            }
        });
    }
}

关于java - 打开另一个窗口时将 JFrame 变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8048416/

相关文章:

java - onclick 取决于 View 所在的位置

java - 多线程弹跳球、显示球问题

java - 重置/删除 Swing 中的边框

java - 无法将 csv 文件中的数据读回数组

java - Ant 到 Gradle,javaCompile 任务的问题

java - izpack 安装程序未实例化

java - 在CardLayout中显示不同的面板尺寸

java - 从另一个类调用另一个 JFrame/JPanel

JQuery 插件,用于逐步增强选择下拉列表到多列显示

javascript - 如何为不同父div下的列动态设置等高