如何在java中更改鼠标光标? [英] How to change the mouse cursor in java?
本文介绍了如何在java中更改鼠标光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JList中有一个单词列表。每当我将鼠标光标指向一个单词时,我希望光标变为手形光标。现在我的问题是如何做到这一点?
I have a list of words inside the JList. Every time I point the mouse cursor at a word, I want the cursor to change into a hand cursor. Now my problem is how to do that?
有人可以帮我解决这个问题吗?
Could someone help me with this problem?
推荐答案
在JList上使用 MouseMotionListener 检测鼠标何时进入,然后调用 setCursor
将其转换为 HAND_CURSOR
。
Use a MouseMotionListener on your JList to detect when the mouse enters it and then call setCursor
to convert it into a HAND_CURSOR
.
示例代码:
final JList list = new JList(new String[] {"a","b","c"});
list.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
final int x = e.getX();
final int y = e.getY();
// only display a hand if the cursor is over the items
final Rectangle cellBounds = list.getCellBounds(0, list.getModel().getSize() - 1);
if (cellBounds != null && cellBounds.contains(x, y)) {
list.setCursor(new Cursor(Cursor.HAND_CURSOR));
} else {
list.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
这篇关于如何在java中更改鼠标光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文