Contoh sederhana dari Event Handling:
------------------------------------------------------------------------------------
MouseEventsDemo.java
package mouseeventsdemo;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Fauzan
*/
public class MouseEventsDemo extends Frame implements MouseListener, MouseMotionListener { // Langkah 1
TextField tf;
public MouseEventsDemo( String title){
super(title);
tf = new TextField(60);
addMouseListener(this); // Langkah 2
}
public void launchFrame() {
/* Menambah komponen pada frame */
add(tf, BorderLayout.SOUTH);
setSize(300,300);
setVisible(true);
}
public void mouseClicked(MouseEvent me) { // Langkah 3
String msg = “Mouse clicked.”;
tf.setText(msg);
}
public void mouseEntered(MouseEvent me) {
String msg = “Mouse entered component.”;
tf.setText(msg);
}
public void mouseExited(MouseEvent me) {
String msg = “Mouse exited component.”;
tf.setText(msg);
}
public void mousePressed(MouseEvent me) {
String msg = “Mouse pressed.”;
tf.setText(msg);
}
public void mouseReleased(MouseEvent me) {
String msg = “Mouse released.”;
tf.setText(msg);
}
public void mouseDragged(MouseEvent me) {
String msg = “Mouse dragged at ” + me.getX()
+ “,” + me.getY();
tf.setText(msg);
}
public void mouseMoved(MouseEvent me) {
String msg = “Mouse moved at ” + me.getX()
+ “,” + me.getY();
tf.setText(msg);
}
}
main.java
package mouseeventsdemo;
/**
*
* @author Ren
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MouseEventsDemo med = new MouseEventsDemo(“Mouse Events Demo”);
med.launchFrame();
}
}
-------------------------------------------------------------
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment