MouseEntered事件为每个附近的标签调用

bas*_*sha 0 java icons swing

我在尝试突出显示更改其图标的'标签'时遇到问题,好吧,因此当为一个jLabel调用MouseEntered事件时,每个附近标签的事件也被调用并且他们的图标正在被更改.我试图通过使用变量来拒绝更改其他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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试在每个事件下使用重置图标来表示不同的jLabel,但是不成功.

Sha*_*hid 5

在你的情况下添加休息我的朋友..添加休息.

               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;
                }
Run Code Online (Sandbox Code Playgroud)

省略break语句会导致每个后续案例被执行.

对于后代,我想指出switch语句是不必要的,因此你遇到的错误是完全可以避免的.例如,您使用了一个实现MouseListener的自定义类,并将您希望在其间转换的图标路径作为参数,当您遇到问题并需要帮助时,您的代码将更容易让其他人遵循.这是一个消除问题根源的示例.

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){
    }
}
Run Code Online (Sandbox Code Playgroud)

很好,您已经编写了正确的实现侦听器的方法.然而,他在他的代码中遇到的问题是没有使用break.如果您使用的是switch-case,则需要使用break.我想这只是一个小姐.