Belajar lagi tentang Event Handling dari dasar.
Berikut ini code untuk handling mouse wheel event.
Nama file MouseWheelDemo.java
import javax.swing.*;
public class MouseWheelDemo extends JFrame {
MouseWheelEventDemo event = new MouseWheelEventDemo();
public MouseWheelDemo(){
setSize(500,500);
JTextArea mainJTextArea = new JTextArea();
mainJTextArea.addMouseWheelListener(event);
add(mainJTextArea);
setVisible(true);
}
public void setFrame(){
setVisible(true);
}
public static void main(String[] args){
MouseWheelDemo x = new MouseWheelDemo();
}
}
Nama file MouseWheelEventDemo.java
import java.awt.event.MouseWheelListener;
public class MouseWheelEventDemo implements MouseWheelListener{
public void mouseWheelMoved(java.awt.event.MouseWheelEvent event) {
System.out.println(“MouseWheelListenerDemo.mouseWheelMoved”);
if (event.getWheelRotation() < 0) {
System.out.println(“Rotated Up ” + event.getWheelRotation());
} else {
System.out.println(“Rotated Down ” + event.getWheelRotation());
}
}
}
Semoga bermanfaat.