Tuesday, March 17, 2009


Changing Mouse Icon Example

In this example, you will learn how to change the cursor icon. In this program, the cursor will change when user moov the mouse to "Yes" button and then "No" button. The source code is available below :

import java.awt.*;
import java.awt.event.*;

public class ChangeCursor{
public static void main(String[] args) {
Frame m = new Frame("Change cursor");
Panel panel = new Panel();
Button b1 = new Button("Yes");
Button b2 = new Button("No");
panel.add(b1);
panel.add(b2);
m.add(panel,BorderLayout.CENTER);
m.setSize(300,300);
m.setVisible(true);
Cursor c1 = b1.getCursor();
Cursor c2 = b2.getCursor();
b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
b2.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
m.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});

}
}


0 comments: