简体   繁体   中英

How would I change the text of a JButton when hovering over it?

I have a music player application that has a repeat button. I want to make it so when the user hovers over the repeat button with their cursor, the text will change to display the current state of the repeat option (off, one, or list). How can I program my button to do this?

Use a MouseListener and the methodes mouseEntered() and mouseExited() .

final JButton btn = new JButton("repeat");
btn.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e) {
        btn.setText("hover");
    }
    @Override
    public void mouseExited(MouseEvent e) {
        btn.setText("repeat");
    }
});

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