如何更改鼠标上的图标?

Luk*_*oni 2 java mouse icons swing mouseover

我有白色图标,当我查看该图标时,我想要制作不同的颜色.

这是我的白色图标:

hint.setIcon(newjavax.swing.ImageIcon(getClass().getResource ("white.png")));
Run Code Online (Sandbox Code Playgroud)

当您将鼠标拖到图标上以更改颜色时,我该怎么做?

And*_*son 8

我怀疑这个问题的答案是在一个未修饰的按钮中.将白色图像设置为图标,将黄色图像设置为翻转图标.像这样:

import java.awt.*;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;

class HoverImage {

    HoverImage(Image img1, Image img2) {
        JButton b = new JButton(new ImageIcon(img1));
        b.setRolloverIcon(new ImageIcon(img2));

        b.setBorderPainted(false);
        b.setContentAreaFilled(false);

        JOptionPane.showMessageDialog(null, b);
    }

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("https://i.stack.imgur.com/XZ4V5.jpg");
        URL url2 = new URL("https://i.stack.imgur.com/7bI1Y.jpg");
        final Image img1 = ImageIO.read(url1);
        final Image img2 = ImageIO.read(url2);
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new HoverImage(img1, img2);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 参看 这个相关的[例子](http://stackoverflow.com/a/4170233/230513). (2认同)