现在我使用下面的代码来绘制一个光标(更大的尺寸):
Cursor emptyCursor = Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), "empty");
setCursor(emptyCursor);
Toolkit toolkit = Toolkit.getDefaultToolkit();
final long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK + AWTEvent.KEY_EVENT_MASK;
final ImageIcon icon = createImageIcon("images/cursor.png");
cursorLabel = new JLabel(icon);
toolkit.addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
MouseEvent me=(MouseEvent)e;
cursorLabel.setLocation(me.getLocationOnScreen().x, me.getLocationOnScreen().y);
}
}, eventMask);
layeredPane = this.getLayeredPane();
if (icon != null) {
cursorLabel.setBounds(15, 225, icon.getIconWidth(), icon.getIconHeight());
} else {
System.err.println("Cursor Icon not found!");
}
layeredPane.add(cursorLabel);
之后我为按钮使用了以下代码:
button.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
button.setBackground(Color.yellow);
}
public void mouseExited(MouseEvent arg0) {
button.setBackground(Color.white);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
光标可以正常工作,但前提是我不按下按钮,因为它是在按钮下方绘制的。有什么问题?