简体   繁体   中英

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 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.

EG 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM