java - 如何在java中更改鼠标光标?

标签 java swing mouse-cursor

我在 JList 中有一个单词列表。每次我将鼠标光标指向一个单词时,我都希望光标变为手形光标。现在我的问题是如何做到这一点?

有人可以帮我解决这个问题吗?

最佳答案

使用 MouseMotionListener在您的 JList 上检测鼠标何时进入它,然后调用 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) {
    }
});

关于java - 如何在java中更改鼠标光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359189/

相关文章:

java - sphinx helloworld 代码错误

java - 使用spring从java执行包含子句 "where col like N' myStr %' "的sql server查询

java - Tomcat 更新事件

java - 当嵌套 JPanel 与 GridBagLayout 一起使用时,布局权重会发生变化

python - TkInter:如何显示正常的光标?

html - 更改复选框 html 的指针

javascript - 如何防止快速鼠标移动在我的绘图应用程序中打断一条线?

java - JTable setRowSelectionInterval 对 JButton 没有影响

java - 从 Unicode 字符串中获取字数(任何语言)

java - 在 jfilechooser 中包含图像缩略图