glassPane 不阻止输入

发布于 2024-11-02 20:00:17 字数 2104 浏览 2 评论 0原文

我用 java 构建了一个小型 GUI 游戏,有时我使用 glassPane 暂时阻止所有鼠标输入。我以前使用过 glassPane 没有任何问题,但这次它不会阻止鼠标输入。因此,当 glassPane 启用时,我仍然可以按下 contentPane 上的按钮,我确信它已启用,因为我可以看到我在其上绘制的内容。

这是一小段可运行的代码,显示了问题:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GuiGame {

    private JPanel contentPane;
    private JButton button;
    private JFrame frame;
    private JPanel glassPane;
    private Dimension screenSize;

    public static void main(String[] args) {
        GuiGame gui = new GuiGame();
        gui.createGUI();
    }

    public void createGUI()
    {
        frame = new JFrame("BadGuiGame!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(400, 400));
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);
        frame.setContentPane(contentPane);
        frame.pack();

        glassPane = new JPanel();
        glassPane.setOpaque(false);
        glassPane.setLayout(null);
        JLabel glassLabel = new JLabel("Glass Enabled");
        glassLabel.setBounds(160, 50, 80, 20);
        glassPane.add(glassLabel);
        frame.setGlassPane(glassPane);

        int buttonWidth = frame.getWidth()/2;
        int buttonHeight = frame.getHeight()/4;
        int xButton = (frame.getWidth() - buttonWidth)/2;
        int yButton = frame.getHeight()/2;
        button = new JButton("NEXT LEVEL!");
        button.setFocusable(false);
        button.setEnabled(true);
        button.setBounds(xButton, yButton, buttonWidth, buttonHeight);
        contentPane.add(button);

        int x = (screenSize.width - frame.getWidth())/2;
        int y = (screenSize.height - frame.getHeight())/2;
        frame.setLocation(x, y);
        frame.setVisible(true);
        glassPane.setVisible(true);
    }
}

I've build a small GUI game in java and at some point I'm using a glassPane to temporarily block all mouseinput. I've used the glassPane before without any problems but this time it won't block the mouseinput. So I can still press a button that resides on the contentPane while the glassPane is enabled, I'm sure it's enabled because I can see the stuff I paint on it.

Here is a short piece of runnable code that's shows the problem:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GuiGame {

    private JPanel contentPane;
    private JButton button;
    private JFrame frame;
    private JPanel glassPane;
    private Dimension screenSize;

    public static void main(String[] args) {
        GuiGame gui = new GuiGame();
        gui.createGUI();
    }

    public void createGUI()
    {
        frame = new JFrame("BadGuiGame!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(400, 400));
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);
        frame.setContentPane(contentPane);
        frame.pack();

        glassPane = new JPanel();
        glassPane.setOpaque(false);
        glassPane.setLayout(null);
        JLabel glassLabel = new JLabel("Glass Enabled");
        glassLabel.setBounds(160, 50, 80, 20);
        glassPane.add(glassLabel);
        frame.setGlassPane(glassPane);

        int buttonWidth = frame.getWidth()/2;
        int buttonHeight = frame.getHeight()/4;
        int xButton = (frame.getWidth() - buttonWidth)/2;
        int yButton = frame.getHeight()/2;
        button = new JButton("NEXT LEVEL!");
        button.setFocusable(false);
        button.setEnabled(true);
        button.setBounds(xButton, yButton, buttonWidth, buttonHeight);
        contentPane.add(button);

        int x = (screenSize.width - frame.getWidth())/2;
        int y = (screenSize.height - frame.getHeight())/2;
        frame.setLocation(x, y);
        frame.setVisible(true);
        glassPane.setVisible(true);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-11-09 20:00:17

我会尝试将 MouseListener 添加到您的玻璃窗格中,并在所有 MouseEvents 上消耗该事件,例如

public void  mouseClicked(MouseEvent e) {
    e.consume();
}

i'd try adding a MouseListener to your glasspane, and on all MouseEvents consume the event, such as

public void  mouseClicked(MouseEvent e) {
    e.consume();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文