How to change cursor icon in Java?

前端 未结 5 1417
误落风尘
误落风尘 2020-12-08 14:06

I would like to change the cursor icon to my customized 32x32 image when a Java application is executing. I looked and searched, those I found are just setting cursor on a J

相关标签:
5条回答
  • 2020-12-08 14:49

    Try settin the cursor on the rootPane.

    frame.getRootPane().setCursor(...);
    
    0 讨论(0)
  • 2020-12-08 14:51

    Call Component.setCursor. The class Cursor as a few predefined cursors.

    A custom cursor image can be created:

    setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
    new ImageIcon("custom.png").getImage(),
    new Point(0,0),"custom cursor"));
    
    0 讨论(0)
  • 2020-12-08 15:01

    Standard cursor image:

    setCursor(Cursor.getDefaultCursor());
    

    User defined Image:

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("icons/handwriting.gif");
    Cursor c = toolkit.createCustomCursor(image , new Point(mainPane.getX(), 
               mainPane.getY()), "img");
    mainPane.setCursor (c);
    

    You can download a zip containing sample source: HERE

    0 讨论(0)
  • 2020-12-08 15:03

    Why don't you have a class MyFrame which exteds JFrame. All it does is call the JFrame constructor and sets the cursor to your desired cursor. In my application we have a touch screen with no cursor so this is how I intend to implement it.

    0 讨论(0)
  • 2020-12-08 15:09
    public void mouseEntered(MouseEvent e)
    {
    // set cursor for frame and its component
    //  this is the current frame you are using .
    //  You can change the this keyword with your frame name .
    
    java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("/images/mousepoint.jpg");
    Cursor a = toolkit.createCustomCursor(image , new Point(this.getX(),this.getY()), "");
    this.setCursor (a);
    }
    
    0 讨论(0)
提交回复
热议问题