pap*_*ski 29 java swing mouse-cursor
我在JList中有一个单词列表.每当我将鼠标光标指向一个单词时,我希望光标变为手形光标.现在我的问题是如何做到这一点?
有人可以帮我解决这个问题吗?
dog*_*ane 36
在JList上使用MouseMotionListener来检测鼠标何时进入它,然后调用setCursor将其转换为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) {
}
});
Run Code Online (Sandbox Code Playgroud)