【问题标题】:MouseEntered event called for every nearby label为附近的每个标签调用 MouseEntered 事件
【发布时间】:2018-09-18 18:35:55
【问题描述】:

我在尝试制作一个更改其图标的突出显示“标签”时遇到问题,好吧,所以当为一个 jLabel 调用 MouseEntered 事件时,附近的每个标签的事件也被调用并且它们的图标被更改。我试图通过使用变量来拒绝更改其他 jLabel 图标来禁用它,但它保持不变,就像它在同一时刻被调用一样,而不让程序将值存储在变量中并执行 if 检查,这是代码:

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;
    }
}

我也尝试在每个事件下为不同的 jLabel 使用重置图标,但没有成功。

【问题讨论】:

  • 您发布的代码没有提示我们您的代码有什么问题。请提供minimal reproducible example,以便我们更好地理解您的意思并对其进行调试以找到您的错误。
  • @SergiyMedvynskyy 建议的制作 MCVE 的提示。例如,获取图像的一种方法是热链接到在 this Q&A 中看到的图像。例如。 This answer 指向嵌入在 this question 中的图像的热链接。

标签: java swing icons


【解决方案1】:

在你的情况下添加中断我的朋友.. 添加中断。

               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;
                }

省略 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){
    }
}

很高兴您编写了实现侦听器的正确方法。然而,他在代码中遇到的问题是没有使用 break。如果您使用的是 switch-case,则需要使用 break。我认为这只是一个错过。

【讨论】:

  • 非常感谢!我从来没有真正看到过正确使用break;在 switch 语句中,感谢您指出。添加休息;每个案例都解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2011-10-08
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 2011-08-10
相关资源
最近更新 更多