2

我有白色图标,当我遇到该图标时,我想制作不同的颜色。

这是我的白色图标:

hint.setIcon(newjavax.swing.ImageIcon(getClass().getResource ("white.png")));

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

4

1 回答 1

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);
            }
        });
    }
}
于 2012-04-13T20:06:20.467 回答