如何更改游标类型

这个问题涉及到以前的帖子。 http://stackoverflow.com/questions/2532936/how-to-save-file-and-read

如何更改光标移动到“手”,只有当鼠标指向网格是不为空(含图片)?

到目前为止光标转到“手”都在网格(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:固定的小问题。

下面是改变光标在JTable中某一列的一种方式:

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

分类:java的 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.771 (s). 10 q(s)