Jes*_*ssy 4 java grid image cursor
这个问题与之前的帖子有关. 如何保存文件和阅读
alt text http://freeimagehosting.net/image.php?dc73c3bb33.jpg
只有当鼠标指向不是Null(包含图像)的网格时,如何才能将光标更改为"Hand"?
到目前为止,光标在整个网格上变为"手"(null或非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);
}
Run Code Online (Sandbox Code Playgroud)
这应该具有预期的效果:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在评论中编辑了新内容的帖子.BorderFactory javadoc:http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html.编辑2:修复小问题.
归档时间: |
|
查看次数: |
13199 次 |
最近记录: |