import java.awt.*;public class test extends JFrame { public test() { try { this.getContentPane().setCursor(getMyCursor());} catch (Exception e) { } this.setSize(640, 500); // 窗口大小为640*500 this.setVisible(true); // 窗口可见 setDefaultCloseOperation(EXIT_ON_CLOSE);}...
java自定义鼠标形状
希望这段代码对你有用
1.自定义个类扩展Cursor,重写Cursor的方法:
class MyCursor extends Cursor {
public MyCursor(int n) {
super(n);
}
static public Cursor getSystemCustomCursor(final String name)
throws AWTException, HeadlessException {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage(name);
return toolkit.createCustomCursor(
image, new Point(0, 0), "myCursor");
}
}
2.在你的程序中:
try {
String ico = "D:\\myCursor.gif";//你图标的位置
this.setCursor(MyCursor.getSystemCustomCursor(ico));
}catch(Exception ae) {
}
-------------------------------------------------------------------
在你的基础上改成下面这个样子就可以了
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.*;
public class test extends JFrame {
public test() {
try {
this.getContentPane().setCursor(getMyCursor());
} catch (Exception e) {
}
this.setSize(640, 500); // 窗口大小为640*500
this.setVisible(true); // 窗口可见
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
test m = new test();
}
public Cursor getMyCursor(){
Image img = new ImageIcon(test.class.getResource("arrow40.cur")).getImage();
return this.getToolkit().createCustomCursor(img,new Point(16,16),"mycursor");
}
}2008-09-17
大概要用到两个方法
一个是设置鼠标样式
setCursor(java.awtCursor.getPredefinedCursor(java.awtCursor.DEFAULT_CURSOR));
一个是建创鼠标样式
public Cursor createCursor(Image img,String name){
return this.getToolkit().createCustomCursor(img,new Point(16,16),name);
}2008-09-17