java 如何更改光标类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2534923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the cursor type
提问by Jessy
This question is related to the previous post. How to save file and read
这个问题与上一篇文章有关。 如何保存文件和读取
alt text http://freeimagehosting.net/image.php?dc73c3bb33.jpg
替代文字 http://freeimagehosting.net/image.php?dc73c3bb33.jpg
How can I change the cursor to "Hand" only when the mouse pointed on grid which is not Null (contained images)?
只有当鼠标指向非空的网格(包含图像)时,如何才能将光标更改为“手”?
So far the cursor turn to "Hand" all over the grids (null or not 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);
}
回答by Chris Dennett
This should have the desired effect:
这应该具有预期的效果:
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;
}
}
Edited post for new stuff in comment. BorderFactory javadoc: http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html. Edit 2: fixed small problem.
在评论中编辑了新内容的帖子。BorderFactory javadoc:http: //java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html。编辑 2:修复小问题。
回答by Abdul Jabbar
Here is one way of changing the cursor at a particular column in JTable:
这是在 JTable 中的特定列更改光标的一种方法:
if(tblExamHistoryAll.columnAtPoint(evt.getPoint()) == 5)
{
setCursor(Cursor.HAND_CURSOR);
}
else
{
setCursor(0);
}

