Java JPanel鼠标侦听器无法在其组件上运行 [英] Java JPanel mouse listener doesn't work over its components

查看:148
本文介绍了Java JPanel鼠标侦听器无法在其组件上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的解决方案是什么?

What is the solution to this problem?

我在JPanel中添加了一个鼠标监听器,但是该面板上装满了覆盖整个JPanel区域的按钮。

I added a mouse listener to a JPanel, but that panel is full of buttons which cover the entire area of the JPanel.

示例(伪)代码:

Create JPanel
Set to GridLayout
Add 25 buttons (5x5 grid)
Add MouseListener to JPanel

MouseListener / MouseMotionListener:

MouseListener/MouseMotionListener:

onMouseMove { print out X,Y co-ords of mouse }

在我到达JPanel容器的边缘之前,合作伙伴永远不会被打印出来,因为按钮阻挡了其余部分

The co-ords are never printed out until I get right to the edge of the JPanel container, because the buttons are blocking the rest of it.

如何让鼠标监听器在所有面板的组件上工作而不必将监听器添加到每个组件 - 或者我应该添加监听器每个组件?

How can I make the mouse listener work over all of the panel's components without having to add the listener to each component - Or am I supposed to add the listener to each component?

添加信息:
我正在尝试为触摸屏系统添加触摸手势(滑动面板caus发生的行动)。
我对自己识别手势非常有信心,但我真的在寻找一种更好的方式,而不是为每个组件添加一个监听器副本(这对我来说更糟糕,因为组件是改变)。

ADDED INFO: I'm trying to add a touch-gesture for a touch-screen system (swiping the panel causes an action to occur). I'm pretty confident about recognising the gesture myself, but I was really looking for a 'better' way than to add a copy of the listener to each component (this would be even worse for me, because the components are changing).

我将尝试将其添加到glassPane而不是......(目前getRootPane()给我NullPointerException)

I am going to try to add it to the glassPane instead... (at the moment getRootPane() gives me NullPointerException)

编辑:
我现在知道我正在尝试从不是根容器的JPanel获取getRootPane(),这就是为什么我会得到a null 。我需要在主JFrame上执行此操作。

I now know that I'm trying to getRootPane() from a JPanel which is not the root container, thats why i'm getting a null. I need to do this on the main JFrame.

EDIT2:
好​​的我已经这样做了(添加了玻璃窗格到主JFrame),起初我遇到了问题,因为我没有这样做:

Okay so I've done that (added glass pane to main JFrame), at first I had a problem because I didn't do this:

myGlassPane.setVisible(true);

所以它似乎无法正常工作。但是,一旦我这样做,我所要做的就是将opaque标志设置为false以使其透明

So it seemed like it wasn't working. But once I did that all I had to do was set the opaque flag to false to make it transparent

myGlassPane.setOpaque(false);

所以现在我正在通过按钮和所有内容打印X,Y合作,但按钮不起作用,因为它们上面有一个面板。

So now i'm getting the X,Y co-ords printed out over the buttons and everything, however the buttons aren't working because there's a panel over them.

推荐答案

用自定义按钮替换JButton。将您需要的事件从按钮转发到按钮的父级。这是一个转发鼠标输入事件的示例。根据需要调整转发并添加异常/错误处理。

Replace JButton with a custom button. Forward the events you need from the button to the button's parent. Here is an example that will forward the mouse entered event. Adjust forwarding and add exception/error handling as necessary.

class JJButton extends JButton {
    {
        addMouseListener(new MouseAdapter(){
        public void mouseEntered(MouseEvent e) {
            if (JJButton.this.getParent()!=null){
                MouseListener[] ml = JJButton.this.getParent().getMouseListeners();
                for (MouseListener l : ml) {
                    l.mouseEntered(e);
                }
            }
        }
        });
    }
    public JJButton(String string) {
        super(string);
    }    
}

这篇关于Java JPanel鼠标侦听器无法在其组件上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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