Java 如何更改光标类型

Java 如何更改光标类型,java,image,grid,cursor,Java,Image,Grid,Cursor,这个问题与上一篇文章有关。 只有当鼠标指向非空的网格(包含图像)时,如何才能将光标更改为“手” 到目前为止,光标在所有网格上都变为“手”(null或NOTNULL) 这应该具有预期的效果: public GUI() { // class attributes protected Component entered = null; protected Border defaultB = BorderFactory...; protected Border hi

这个问题与上一篇文章有关。

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

到目前为止,光标在所有网格上都变为“手”(null或NOTNULL)


这应该具有预期的效果:

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:。编辑2:修复了小问题。

以下是更改JTable中特定列上光标的一种方法:

if(tblExamHistoryAll.columnAtPoint(evt.getPoint()) == 5)
{
     setCursor(Cursor.HAND_CURSOR);
}
else
{
     setCursor(0);
}

我还想在“图像边框”上添加高光效果,这样效果更明显。我该怎么做?查看更新的帖子:)你可以用给定的代码调整到你的心满意足。谢谢,这是可行的,但边框保持在那里,甚至没有删除图像外的光标:-(修复了一个小问题,没有设置“entered”。谢谢..:-)受保护的边框defaultB=BorderFactory.createEmptyBorder();我添加了这段代码,它只在图像中突出显示图像。。非常感谢:-)
if(tblExamHistoryAll.columnAtPoint(evt.getPoint()) == 5)
{
     setCursor(Cursor.HAND_CURSOR);
}
else
{
     setCursor(0);
}