如何添加JLabel图像悬停?

3

我应该如何为JLabel添加悬停效果?比如当你将鼠标移动到JLabel上时,会出现新的图像覆盖在它上面。我知道如何让按钮做到这一点,但是同样的技术不适用于JLabel。请问有人可以指导我如何实现JLabel的悬停效果吗?谢谢。

package src;

import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
 * @Author - 0x29A
 * 
 * 
 */
public class Jframe {

    public static void main(final String args[]) {

        /*
         * @Images
         */
        final ImageIcon icon = new ImageIcon("Data/button.png");
        final JLabel label = new JLabel(icon);

        final ImageIcon icon1 = new ImageIcon("Data/button1.png");
        final JLabel label1 = new JLabel(icon1);

        final ImageIcon icon2 = new ImageIcon("Data/button2.png");
        final JLabel label2 = new JLabel(icon2);

        final ImageIcon icon3 = new ImageIcon("Data/button3.png");
        final JLabel label3 = new JLabel(icon3);

        final ImageIcon icon4 = new ImageIcon("Data/button4.png");
        final JLabel label4 = new JLabel(icon4);

        final ImageIcon icon5 = new ImageIcon("Data/button5.png");
        final JLabel label5 = new JLabel(icon5);

        final ImageIcon icon6 = new ImageIcon("Data/background.png");
        final JLabel label6 = new JLabel(icon6);

        /*
         * @Image Location
         */
        label.setBounds(282, 255, 96, 96);
        label1.setBounds(384, 255, 96, 96);
        label2.setBounds(282, 153, 96, 96);
        label3.setBounds(384, 153, 198, 96);
        label4.setBounds(181, 152, 96, 96);
        label5.setBounds(181, 255, 96, 96);
        label6.setBounds(0, 0, 765, 503);

        /*
         * @Frame
         */
        final JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(765, 503));
        frame.setLayout(null);
        frame.add(label);
        frame.add(label1);
        frame.add(label2);
        frame.add(label3);
        frame.add(label4);
        frame.add(label5);
        frame.add(label6);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

1
我会建议先学习如何使用MouseListener和MouseMotionListener,这个这个教程可以帮助你。你已经尝试过什么了吗? - Hovercraft Full Of Eels
谢谢您提供的信息,但问题是我不知道如何将它们与我已有的代码结合使用。您能否为我的一个标签添加鼠标事件,然后我可以跟着您的示例自己添加其余的内容?这将非常感激。 - 0x29A
我的理念是,如果你至少先自己尝试一下,你会获得更多。此外,通过看到你可能有的错误假设,我们将能够更好地帮助你。最后,试一试也不会有任何损失。 - Hovercraft Full Of Eels
1
满载电鳗的气垫船,也谢谢您的帮助 :) - 0x29A
2个回答

11

我做了一个快速的例子,它使用MouseListenerMosueAdapter来监视JLabel上的mouseExited()mouseEntered()事件,并且当这些方法中的任何一个被调用时(即鼠标在标签上方或不在其上方时),图片会被更改:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import javax.swing.*;

public class LabelHoverTest extends JFrame {

    Icon pic1;
    Icon pic2;
    JLabel label;

    public LabelHoverTest(String title) {
        super(title);
        pic1 = UIManager.getIcon("OptionPane.informationIcon");
        pic2 = UIManager.getIcon("OptionPane.questionIcon");
        createAndShowUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LabelHoverTest("Label Hover Test").setVisible(true);
            }
        });
    }

    private void createAndShowUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        addComponentsToPane(getContentPane());
        setSize(300, 300);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void addComponentsToPane(Container contentPane) {
        label = new JLabel(pic1);

        contentPane.add(label, BorderLayout.CENTER);

        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                label.setIcon(pic2);
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                label.setIcon(pic1);
            }
        });
    }
}

2
+1 例子;UIManager.getIcon,在这里展示,可能更加自包含;pack() - trashgod
1
@trashgod很棒,我已经把它加入到我的答案中了 :) - David Kroukamp

3
您可以使用 MouseEntered 鼠标事件,并编写以下代码来实现此功能:JLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("图片位置")));

+1 在我回答之后才看到这个。好建议 :) - David Kroukamp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接