Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在鼠标下检查组件_Java_Swing - Fatal编程技术网

Java 在鼠标下检查组件

Java 在鼠标下检查组件,java,swing,Java,Swing,我在桌面Java项目中使用了SWING 可以检查鼠标下的组件类型吗 例如: 我有许多不同的组件,我想把鼠标放在上面,得到什么组件下(例如JButton,ComboBox等)。有可能吗 我不想将鼠标侦听器添加到所有组件。也许有办法在整个面板上添加鼠标侦听器 我不想将鼠标侦听器添加到所有组件 您可以使用AWTEventListener侦听生成的所有事件 查看示例以侦听MouseeEvents和KeyEvents 我不想将鼠标侦听器添加到所有组件 您可以使用AWTEventListener侦听生成的所

我在桌面Java项目中使用了SWING

可以检查鼠标下的组件类型吗

例如: 我有许多不同的组件,我想把鼠标放在上面,得到什么组件下(例如JButton,ComboBox等)。有可能吗

我不想将鼠标侦听器添加到所有组件。也许有办法在整个面板上添加鼠标侦听器

我不想将鼠标侦听器添加到所有组件

您可以使用
AWTEventListener
侦听生成的所有事件

查看示例以侦听MouseeEvents和KeyEvents

我不想将鼠标侦听器添加到所有组件

您可以使用
AWTEventListener
侦听生成的所有事件


查看示例以收听鼠标事件和按键事件。

此问题分为两部分

1。获取鼠标事件

正如@carmickr所说,使用AWTEventListener,比如so
Toolkit.getDefaultToolkit().addAWTEventListener(listener,eventMask)
,并使用事件掩码仅过滤鼠标事件

2。检查鼠标组件

您需要获取GUI组件层次结构中的所有组件,并检查鼠标是否位于每个组件内部。嵌套最深的是鼠标所在的位置。以下是一种获取所有组件的方法(如果有帮助):

public static List<Component> getAllComponents(final Container c) {
    Component[] comps = c.getComponents();
    List<Component> compList = new ArrayList<Component>();
    for (Component comp : comps) {
        compList.add(comp);
        if (comp instanceof Container)
            compList.addAll(getAllComponents((Container) comp));
    }
    return compList;
}
公共静态列表getAllComponents(最终容器c){
组件[]comps=c.getComponents();
List compList=new ArrayList();
用于(组件组件:组件){
compList.add(comp);
if(容器的组件实例)
addAll(getAllComponents((容器)comp));
}
返回compList;
}

遵循这两个步骤将确保您不会与正在使用的任何其他事件处理程序发生冲突。

此问题分为两部分

1。获取鼠标事件

正如@carmickr所说,使用AWTEventListener,比如so
Toolkit.getDefaultToolkit().addAWTEventListener(listener,eventMask)
,并使用事件掩码仅过滤鼠标事件

2。检查鼠标组件

您需要获取GUI组件层次结构中的所有组件,并检查鼠标是否位于每个组件内部。嵌套最深的是鼠标所在的位置。以下是一种获取所有组件的方法(如果有帮助):

public static List<Component> getAllComponents(final Container c) {
    Component[] comps = c.getComponents();
    List<Component> compList = new ArrayList<Component>();
    for (Component comp : comps) {
        compList.add(comp);
        if (comp instanceof Container)
            compList.addAll(getAllComponents((Container) comp));
    }
    return compList;
}
公共静态列表getAllComponents(最终容器c){
组件[]comps=c.getComponents();
List compList=new ArrayList();
用于(组件组件:组件){
compList.add(comp);
if(容器的组件实例)
addAll(getAllComponents((容器)comp));
}
返回compList;
}

遵循这两个步骤将确保您不会与正在使用的任何其他事件处理程序发生冲突。

已解决

正如@Eric Bischoff所说,我使用了event
AWTEvent.MOUSE\u event\u MASK
和AWTEventListener

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            if (e.getSource() instanceof JButton) {
                JButton button = (JButton) e.getSource();

                Log(button.getText());
            }
        }
    }, AWTEvent.MOUSE_EVENT_MASK);

使用
e.getSource()
I获取事件处于活动状态的组件,检查它是否是JButton的实例,下一个转换到JButton,并且可以做一些好事,例如在这个按钮上执行的操作(不要浪费时间通过
按钮.doClick()单击它)。我这样做是为了优化我的应用程序,因为
button.doClick()
花费了大约80毫秒,但是当我们从这个按钮对事件执行操作时,这个花费是0毫秒(从委托到监听器在这个按钮上执行操作的时间)。

解决了

正如@Eric Bischoff所说,我使用了event
AWTEvent.MOUSE\u event\u MASK
和AWTEventListener

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            if (e.getSource() instanceof JButton) {
                JButton button = (JButton) e.getSource();

                Log(button.getText());
            }
        }
    }, AWTEvent.MOUSE_EVENT_MASK);

使用
e.getSource()
I获取事件处于活动状态的组件,检查它是否是JButton的实例,下一个转换到JButton,并且可以做一些好事,例如在这个按钮上执行的操作(不要浪费时间通过
按钮.doClick()单击它)。我这样做是为了优化我的应用程序,因为
button.doClick()
大约需要80毫秒,但是当我们从这个按钮对事件执行操作时,这个成本是0毫秒(从委托到监听器在这个按钮上执行操作的时间)。

检查一下:你能解释一下你对在每个组件中添加鼠标侦听器的反感吗?您只需填写一个mouseenterned/MouseExited方法并将其添加到所有组件中即可。什么这么麻烦?这很麻烦,因为我在许多不同的面板中有数千个组件,而不是在所有组件中添加鼠标侦听器,我想到了一些类似于鼠标下的验证组件的东西。在包含此组件的面板上不使用MouseAdapter是可能的?请检查:您能解释一下您对在每个组件上添加MouseAdapter的反感吗?您只需填写一个mouseenterned/MouseExited方法并将其添加到所有组件中即可。什么这么麻烦?这很麻烦,因为我在许多不同的面板中有数千个组件,而不是在所有组件中添加鼠标侦听器,我想到了一些类似于鼠标下的验证组件的东西。在包含此组件的面板上不使用MouseAdapter是可能的?好的,但如何获取面板中的所有组件?所有组件都在面板上,我没有任何组件列表。如何从我的面板中获取此容器?我想打开监听,获取JButton对象,然后关闭监听。我接受你的答案,因为这是最糟糕的:)我发布了我的解决方案,让JButton在他们身上执行操作OK,但如何获取面板中的所有组件?所有组件都在面板上,我没有任何组件列表。如何从我的面板中获取此容器?我想打开监听,获取JButton对象并关闭监听。我接受你的答案,因为这是错误的:)我发布了获取JButton对象并对其执行操作的解决方案