Java 在mouseMove上设置jLabel图标,并随时间更改图像

Java 在mouseMove上设置jLabel图标,并随时间更改图像,java,swing,netbeans,jlabel,imageicon,Java,Swing,Netbeans,Jlabel,Imageicon,我使用的是netbeans,我有一个jLabel,当鼠标重叠在按钮上时(当您要单击时),图标会发生变化。我的代码: private void BtnRoomsMouseMoved(java.awt.event.MouseEvent evt) { jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/at/rosom.jpg"))); }

我使用的是netbeans,我有一个jLabel,当鼠标重叠在按钮上时(当您要单击时),图标会发生变化。我的代码:

private void BtnRoomsMouseMoved(java.awt.event.MouseEvent evt) {                                    
   jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/at/rosom.jpg")));
}    
我想让它改变的时间,只要鼠标仍然重叠在按钮上。这使它看起来像一个屏幕保护程序。
我的想法是我应该使用
Thread.sleep()
,但我不知道它的正确用法。请帮忙

不要尝试使用
鼠标侦听器
只需使用按钮翻转功能即可

button.setIcon(icon);
button.setRolloverEnabled(true);

// create an instance of the RolloverIcon class when calling setRolloverIcon
button.setRolloverIcon(rollOverIcon);
不需要定时器什么的。运行简单的示例

import java.net.*;
import java.util.logging.*;
import javax.swing.*;

public class ButtonRollover {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    ImageIcon rollover = new ImageIcon(new URL("https://trustcloud.com/images/homepage-icon-source-stackoverflow.png"));
                    ImageIcon icon = new ImageIcon(new URL("http://www.pixelearth.net/images/icons/stackoverflow.png"));

                    JButton button = new JButton(icon);
                    button.setRolloverEnabled(true);
                    button.setRolloverIcon(rollover);

                    JOptionPane.showMessageDialog(null, button, "Rollover", JOptionPane.PLAIN_MESSAGE);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ButtonRollover.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        });
    }
}

编辑

从这个
btnRoomMouseMoved
的外观来看,当您应该使用
MouseListener
时,您似乎在使用
MouseListener
。右键单击按钮时,不要选择鼠标情感,而是选择鼠标情感,然后您需要同时执行鼠标退出和鼠标退出。您可以在这些方法中来回更改标签图标

运行此示例

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.*;
import java.util.logging.*;
import javax.swing.*;

public class ButtonRollover {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final ImageIcon rollover = new ImageIcon(new URL("https://trustcloud.com/images/homepage-icon-source-stackoverflow.png"));
                    final ImageIcon icon = new ImageIcon(new URL("http://www.pixelearth.net/images/icons/stackoverflow.png"));

                    final JLabel label = new JLabel(icon);

                    JButton button = new JButton("Change Image");
                    button.addMouseListener(new MouseAdapter(){
                        public void mouseEntered(MouseEvent e) {
                            label.setIcon(rollover);
                        }
                        public void mouseExited(MouseEvent e) {
                            label.setIcon(icon);
                        }
                    });

                    JPanel panel = new JPanel();
                    panel.add(label);
                    panel.add(button);

                    JOptionPane.showMessageDialog(null, panel, "Rollover", JOptionPane.PLAIN_MESSAGE);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ButtonRollover.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

在哪里把它放在netbeans上?在你的构造函数中创建图标,然后执行上面的操作。请详细解释。我是个很慢的人。对不起,首先你知道什么是构造函数吗?你知道如何创建一个ImageIcon吗?不知道构造函数是什么。在netbeans中,我只需转到属性和设置图标