简体   繁体   English

将鼠标悬停在它上面时,如何更改JButton的文本?

[英]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() . 使用MouseListener和方法mouseEntered()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");
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将不需要的文本悬停在JButton上 - Unwanted text appearing over JButton when hovering over it 通过将鼠标悬停在 label 上,我将如何获得解释 label 是什么的 JLabel 的文本(带有图标)? - How would I get the text of a JLabel (with an icon) explaining what the label is by hovering over the label? 如何在每次单击按钮时使用JButton更改JLabel? - How would I use a JButton to change a JLabel with every click of a button? 如何在我将鼠标悬停在 JButton 上时更改它的颜色,但即使我在单击它之后悬停时将其永久更改为其他颜色? - How to change a JButton color when I hover over it but change it permanently to something else even if I hover afterwards when I click it? 鼠标悬停时如何在Java中更改背景扩展JButton - How to change background an extended JButton in java when mouse over 如何将鼠标监听器添加到JTree,以便在将鼠标悬停在节点上时可以更改光标(到手形光标)? - How to add a mouse listener to a JTree so that I can change the cursor (to a hand cursor) when hovering over a node? 预熔。 将鼠标悬停在节点上时,如何更改连接边的可视化效果? - Prefuse. When hovering over a node, how can I change the connected edges' visualization? 如何更改JButton的文本颜色 - How to change text color of a JButton 如何在Java中更改实际JButton的颜色? 不是背景,不是文字 - How do I change the color of an actual JButton in Java? Not the background and not the text 如何通过单击我的JButton来更改JLabel文本? - How do I change the JLabel text by clicking on my JButton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM