【发布时间】: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 中的图像的热链接。