Berikut ini contoh program penanganan event terkait mouse. Terdapat
dua listener terkait dengan event mouse yaitu MouseListener dan
MouseMotionListener.
Berikut ini tampilannya:
Berikut ini program lengkapnya:
02 | import java.awt.event.*; |
05 | public class MouseEventHandling extends JFrame |
06 | implements MouseListener, MouseMotionListener { |
08 | private JLabel statusBar; |
10 | public MouseEventHandling () { |
11 | super ( "Mencoba Beberapa Mouse Event Handling" ); |
13 | statusBar = new JLabel(); |
14 | getContentPane().add(statusBar,BorderLayout.SOUTH); |
16 | addMouseListener ( this ); |
17 | addMouseMotionListener ( this ); |
20 | setLocationRelativeTo( null ); |
25 | public void mouseClicked (MouseEvent e) { |
26 | statusBar.setText( "Clicked at [" + e.getX() + "," + e.getY() + "]" ); |
29 | public void mousePressed (MouseEvent e) { |
30 | statusBar.setText( "Pressed at [" + e.getX() + "," + e.getY() + "]" ); |
33 | public void mouseReleased (MouseEvent e) { |
34 | statusBar.setText( "Released at [" + e.getX() + "," + e.getY() + "]" ); |
37 | public void mouseEntered (MouseEvent e) { |
38 | statusBar.setText( "Entered at [" + e.getX() + "," + e.getY() + "]" ); |
39 | getContentPane().setBackground(Color.GREEN); |
42 | public void mouseExited (MouseEvent e) { |
43 | statusBar.setText( "Mouse outside window" ); |
44 | getContentPane().setBackground(Color.WHITE); |
48 | public void mouseDragged (MouseEvent e) { |
49 | statusBar.setText( "Dragged at [" + e.getX() + "," + e.getY() + "]" ); |
52 | public void mouseMoved (MouseEvent e) { |
53 | statusBar.setText( "Moved at [" + e.getX() + "," + e.getY() + "]" ); |
56 | public static void main (String args[]) { |
57 | MouseEventHandling test = new MouseEventHandling(); |
58 | test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
0 :
Posting Komentar