开发者

How do I Highlight buttons when mouse pointer is hovered upon them in Java(Like Start button highlights in XP)?

If mouse pointer is hovered on start button in Windows XP, it highlights. I want to do the same in Java. Can anyone help开发者_StackOverflow中文版 me?


A possible solution is to create an Icon for your button to be displayed on rollover and then add it to the button via its setRolloverIcon. The mouse's model will do all that is needed to display this Icon.

And even another solution is to add a ChangeListener to the button's model. In the listener if isRollover() returns true, change the display.

E.G. of 1st technique.

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

class ButtonRollover {
    public static void main(String[] args) throws Exception {
        URL imageUrl2 = new URL("http://pscode.org/media/stromlo2.jpg");
        URL imageUrl1 = new URL("http://pscode.org/media/stromlo1.jpg");

        final Image image2 = ImageIO.read(imageUrl2);
        final Image image1 = ImageIO.read(imageUrl1);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JButton button = new JButton("Hover Me!");

                button.setIcon(new ImageIcon(image2));
                button.setRolloverIcon(new ImageIcon(image1));

                JOptionPane.showMessageDialog(null, button);
            }
        });
    }
}


Is this for a web page? If so, you can easily do this sort of thing with CSS.

Further information is required, really.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜