gpt4 book ai didi

java - 如何更改光标类型

转载 作者:搜寻专家 更新时间:2023-11-01 01:48:15 26 4
gpt4 key购买 nike

这个问题与上一篇文章有​​关。 How to save file and read

alt text http://freeimagehosting.net/image.php?dc73c3bb33.jpg

只有当鼠标指向不是 Null 的网格(包含图像)时,如何才能将光标更改为“手”?

到目前为止,光标在所有网格上都变成了“手”(空或非空)。

public GUI() {
....
JPanel pDraw = new JPanel();
....
for(Component component: pDraw.getComponents()){
JLabel lbl = (JLabel)component;

//add mouse listener to grid box which contained image
if (lbl.getIcon() != null)
lbl.addMouseListener(this);
}

public void mouseEntered(MouseEvent e) {
Cursor cursor = Cursor.getDefaultCursor();
//change cursor appearance to HAND_CURSOR when the mouse pointed on images
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
setCursor(cursor);
}

最佳答案

这应该有预期的效果:

public GUI() {
// class attributes
protected Component entered = null;
protected Border defaultB = BorderFactory...;
protected Border highlighted = BorderFactory...;

....
JPanel pDraw = new JPanel();
....
for(Component component: pDraw.getComponents()){
JLabel lbl = (JLabel)component;

//add mouse listener to grid box which contained image
if (lbl.getIcon() != null)
lbl.addMouseListener(this);
}

public void mouseEntered(MouseEvent e) {
if (!(e.getSource() instanceof Component)) return;
exit();
enter((Component)e.getSource());
}

public void mouseExited(MouseEvent e) {
exit();
}

public void enter(Component c) {
//change cursor appearance to HAND_CURSOR when the mouse pointed on images
Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
setCursor(cursor);
c.setBorder(highlighted);
entered = c;
}

public void exit() {
Cursor cursor = Cursor.getDefaultCursor();
setCursor(cursor);
if (entered != null) {
entered.setBorder(defaultB);
entered = null;
}
}

为评论中的新内容编辑帖子。 BorderFactory javadoc:http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html .编辑 2:修复了小问题。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com