简体   繁体   English

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

[英]Hover over multiple buttons in Java?

Is it possible, in Java, when you hover over a single button, to make the program think you are hovering over multiple buttons? 在Java中,当您将鼠标悬停在一个按钮上时,是否有可能让程序认为您正在将鼠标悬停在多个按钮上? I'm using a multi-dimensional array with buttons and want to be able to have 5 buttons be hovered over at a time. 我正在使用带按钮的多维数组,并且希望能够一次悬停5个按钮。 (All the buttons near the actual hover). (实际悬停附近的所有按钮)。

Any ideas on how to do this? 关于如何做到这一点的任何想法?

Note: I'm not using JButtons, just regular buttons. 注意:我没有使用JButtons,只使用常规按钮。 (awt.Button) (awt.Button)

EDIT I obviously wasn't clear enough, and I apologize for that. 编辑我显然不够清楚,我为此道歉。 Here is a screenshot of what I'm looking for: 这是我正在寻找的截图:

徘徊

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

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. 假设您正在使用MouseListener ,当在主按钮上调用mouseEntered(MouseEvent e)方法时,在所有其他按钮的所有侦听器上显式调用相同的方法,传递您已经给出的事件。 Ditto for the mouseExited(MouseEvent e) method. 同样适用于mouseExited(MouseEvent e)方法。

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> . 没有理由不能将MouseListener的引用保存到List<Button> If it's the business of the listener to work on those buttons then design your classes so that it happens. 如果是侦听器的业务来处理这些按钮,那么设计你的类以便它发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM