我在尝试制作一个更改其图标的突出显示“标签”时遇到问题,好吧,所以当为一个 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 使用重置图标,但没有成功。
最佳答案
在你的情况下添加中断,我的 friend ..添加中断。
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。我认为这只是一个错过。
关于java - MouseEntered 事件调用每个附近的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52392708/