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

时间:2014-02-05 12:30:48

标签: 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")));
}    

只要鼠标仍然重叠在按钮上,我想让它按时更改。这使它看起来像一个屏幕保护程序。 我的想法是我应该使用Thread.sleep(),但我不知道它是否正确使用。请帮忙。

1 个答案:

答案 0 :(得分:1)

只需使用按钮翻转功能

,而不是尝试使用MouseListener
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);
                }
            }

        });
    }
}

修改

根据BtnRoomsMouseMoved的外观,当您使用MouseMotionListener时,您似乎正在使用MouseListener。右键单击按钮,而不是选择MouseMotion,选择Mouse,您需要同时实施mouseEnteredmouseExited。您可以在这些方法中来回更改标签图标。

运行此示例

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