2012-04-13 52 views
2

我得到了白色的图标,当我穿过那个图标时,我想做出不同的颜色。如何更改鼠标上的图标?

这里是我的图标是白色:

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

我怎么能当你拖动鼠标移到图标改变颜色做什么呢?

回答

8

我怀疑这个问题的答案是在未修饰的按钮中。将白色图像设置为图标,将黄色图像设置为roll-over icon。像这样:

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

参考这个相关的[示例](http://stackoverflow.com/a/4170233/230513)。 – trashgod 2012-04-13 20:43:42

+0

'Hint.setRolloverIcon(“yellow.png”)';我使用该代码,它的工作原理是corectlly – 2013-10-28 13:08:35