将鼠标悬停在 Java 中的多个按钮上?

发布于 2024-10-17 04:59:46 字数 376 浏览 3 评论 0原文

在Java中,当您将鼠标悬停在单个按钮上时,是否有可能使程序认为您将鼠标悬停在多个按钮上? 我正在使用带有按钮的多维数组,并且希望能够一次将 5 个按钮悬停在其上。 (实际悬停附近的所有按钮)。

关于如何做到这一点有什么想法吗?

注意:我没有使用 JButton,只是使用常规按钮。 (awt.Button)

编辑 显然是我表达得不够清楚,对此我深表歉意。 这是我正在寻找的内容的屏幕截图:

Hover

因此,光标悬停在第一个灰色空间上,并且它旁边的所有空间都有不同的背景,但是,它们不被视为悬停在上面,如果我需要的话。

Is it possible, in Java, when you hover over a single button, to make the program think you are hovering over multiple buttons?
I'm using a multi-dimensional array with buttons and want to be able to have 5 buttons be hovered over at a time. (All the buttons near the actual hover).

Any ideas on how to do this?

Note: I'm not using JButtons, just regular buttons. (awt.Button)

EDIT
I obviously wasn't clear enough, and I apologize for that.
Here is a screenshot of what I'm looking for:

Hover

So, the cursor is hovering over the first gray space, and all of the space next to it have a different background, however, they are not considered as being hovered over, which if what I need.

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

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

发布评论

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

评论(1

哭泣的笑容 2024-10-24 04:59:46

假设您使用的是 MouseListener,当在主按钮上调用 mouseEntered(MouseEvent e) 方法时,在所有按钮的所有侦听器上显式调用相同的方法。其他按钮,传递给您的事件。 mouseExited(MouseEvent e) 方法也是如此。

您需要维护从主按钮到从属按钮的引用。

从属按钮的侦听器将接收引用主按钮的事件。如有必要,请创建监听器并引用它们所附加的按钮,以便您可以在接收事件时对该按钮进行操作。

编辑:

这就是我正在谈论的事情。有帮助吗?

final List<Button> subordinateButtons = Arrays.asList(new Button(), new Button(), new Button());
Button myButton = new Button();
myButton.addMouseListener(new MouseListener() {

    public void mouseEntered(MouseEvent e) {
        for (Button subordinateButton : subordinateButtons) {
            subordinateButton.setBackground(Color.GRAY);
        }
    }

    public void mouseExited(MouseEvent e) {
        for (Button subordinateButton : subordinateButtons) {
            subordinateButton.setBackground(Color.LIGHT_GRAY);
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

});

您没有理由不能保留从 MouseListenerList

Assuming you are using a MouseListener, when the mouseEntered(MouseEvent e) method is called on the master button, explicitly call the same method on all of the listeners of all of the other buttons, passing the event you have been given. Ditto for the mouseExited(MouseEvent e) method.

It's up to you to maintain a reference from the master button to the subordinate buttons.

The subordinate buttons' listeners will receive an event that refers to the master button. If necessary, create your listeners with a reference to the button that they are attached to, so that you can operate on that button when receiving an event.

EDIT:

This is the kind of thing I'm talking about. Does it help?

final List<Button> subordinateButtons = Arrays.asList(new Button(), new Button(), new Button());
Button myButton = new Button();
myButton.addMouseListener(new MouseListener() {

    public void mouseEntered(MouseEvent e) {
        for (Button subordinateButton : subordinateButtons) {
            subordinateButton.setBackground(Color.GRAY);
        }
    }

    public void mouseExited(MouseEvent e) {
        for (Button subordinateButton : subordinateButtons) {
            subordinateButton.setBackground(Color.LIGHT_GRAY);
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

});

There's no reason why you can't keep a reference from a MouseListener to a List<Button>. If it's the business of the listener to work on those buttons then design your classes so that it happens.

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