Results 1 to 1 of 1
-
Demonstrating the MouseWheelListener
Java Code:import java.awt.Color; import java.awt.Container; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.JFrame; public class MouseWheelTest extends JFrame { private static final Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW }; public MouseWheelTest() { super(); final Container contentPane = getContentPane(); MouseWheelListener listener = new MouseWheelListener() { int colorCounter; private static final int UP = 1; private static final int DOWN = 2; public void mouseWheelMoved(MouseWheelEvent e) { int count = e.getWheelRotation(); int direction = (Math.abs(count) > 0) ? UP : DOWN; changeBackground(direction); } private void changeBackground(int direction) { contentPane.setBackground(colors[colorCounter]); if (direction == UP) { colorCounter++; } else { --colorCounter; } if (colorCounter == colors.length) { colorCounter = 0; } else if (colorCounter < 0) { colorCounter = colors.length - 1; } } }; contentPane.addMouseWheelListener(listener); } public static void main(String args[]) { JFrame frame = new MouseWheelTest(); frame.setSize(300, 300); frame.show(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Demonstrating the ItemListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:25 PM -
Demonstrating the InternalFrameListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:24 PM -
Demonstrating the HyperlinkListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:23 PM -
Demonstrating the FocusListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:23 PM -
Demonstrating the WeakHashMap
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks