简体   繁体   English

每个附近标签都会调用MouseEntered事件

[英]MouseEntered event called for every nearby label

I am having issues while trying to make a highlighting 'label' that changes its icon, okay, so when MouseEntered event is being called for one jLabel, every nearby label's event is also being called and their icon is being changed. 我在尝试制作突出显示的“标签”以更改其图标时遇到问题,好吧,所以当为一个jLabel调用MouseEntered事件时,附近的每个标签的事件也都被调用,并且它们的图标也被更改。 I've tried to disable that by using variable to deny changing other jLabel icons but it remains the same like it's being called at the same moment without letting the program storing values in variable and performing if checks, here's the code: 我试图通过使用变量来拒绝更改其他jLabel图标来禁用它,但是它仍然像在同一时刻被调用一样,而无需让程序将值存储在变量中并执行是否检查,这是代码:

private int OverlayButton = -1;

private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {                                     
    SetButton( 1 );
}                                    

private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {                                    
    ResetButton( 1 );
}                                   

private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {                                     
    SetButton( 2 );
}                                    

private void jLabel2MouseExited(java.awt.event.MouseEvent evt) {                                    
    ResetButton( 2 );
}                                   

private void jLabel3MouseEntered(java.awt.event.MouseEvent evt) {                                     
    SetButton( 3 );
}                                    

private void jLabel3MouseExited(java.awt.event.MouseEvent evt) {                                    
    ResetButton( 3 );
}    

public void SetButton( int button ) {

    if( OverlayButton == -1 ) {
        OverlayButton = button;
        System.out.println( "SetButton method | (BUTTON-ID:"+ button+ ") ." );
        switch( button ) {
            case 1:         {
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryCalendar.png")));
            }
            case 2:         {
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryNotification.png")));
            }
            case 3:         {
                jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/medal (1).png")));
            }
            case 4:         {

            }
        }
    }
    else {}
}

public void ResetButton( int button ) {

    if( OverlayButton != -1 ) {
        switch( button ) {
            case 1:         {
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/calendar-with-a-clock-time-tools.png")));
            }
            case 2:         {
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/notifications-button.png")));
            }
            case 3:         {
                jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/medal (2).png")));
            }
        }
        System.out.println( "ResetButton method | (BUTTON-ID:"+ button+ ") | Setting OverlayButton to -1." );
        OverlayButton = -1;
    }
}

I've also tried using resetting icons under each event for different jLabels, but unsuccessfuly. 我还尝试过在每个事件下为不同的jLabel使用重置图标,但未成功。

Add break in your case my friend.. Add break. 在您的情况下,我的朋友要加个休息点。

               case 1: {
                    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryCalendar.png")));
                    break;
                }
                case 2:         {
                    jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryNotification.png")));
                    break;
                }
                case 3:         {
                    jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/medal (1).png")));
                    break;
                }

Omitting the break statement cause each subsequent case to be executed. 省略break语句将导致执行每个后续案例。

For posterity, I want to point that the the switch statement was unnecessary and therefore the error you encountered was completely avoidable. 为了后代,我想指出,switch语句是不必要的,因此您遇到的错误是完全可以避免的。 Had you used, for one example, a custom class that implements MouseListener and takes the icon paths you wish to transition between as arguments your code would have been easier for others to follow when you have questions and need help. 例如,如果您使用了一个自定义类,该类实现了MouseListener并采用了您希望在其间转换的图标路径作为参数,那么当您有疑问和需要帮助时,其他人就可以更轻松地遵循自己的代码。 Here's an example that eliminates the source of your problem. 这是一个消除问题根源的示例。

public static class LabelListener implements MouseListener {
    private ImageIcon newIcon;
    private ImageIcon defaultIcon;
    private JLabel label;
    public LabelListener(JLabel label, String newIconPath, String defaultIconPath) throws IOException{
            this.label = label;
            this.label.setSize(100, 100);
            this.newIcon = new ImageIcon(newIconPath);
            this.defaultIcon = new ImageIcon(defaultIconPath);
            this.label.setIcon(this.defaultIcon);
    }
    @Override
    public void mouseEntered(MouseEvent evt){
            this.label.setIcon(this.newIcon);
    }
    @Override
    public void mouseExited(MouseEvent evt){
            this.label.setIcon(this.defaultIcon);
    }
    @Override
    public void mousePressed(MouseEvent evt){
    }
    @Override
    public void mouseClicked(MouseEvent evt){
    }
    @Override
    public void mouseReleased(MouseEvent evt){
    }
}

Good that you have written the correct way of implementing the listener. 很好,您编写了实现侦听器的正确方法。 However the problem that he had in his code is of not using break. 但是,他在代码中遇到的问题是没有使用break。 If you are using switch-case, you need to use break. 如果使用开关盒,则需要使用break。 I think it is just a miss. 我认为这只是一个错过。

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

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