[英]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.