Home | Looking for something? Sign In | New here? Sign Up | Log out

Minggu, 06 Januari 2013

Mouse Event di Java

Minggu, 06 Januari 2013

Berikut ini contoh program penanganan event terkait mouse. Terdapat dua listener terkait dengan event mouse yaitu MouseListener dan MouseMotionListener.
Berikut ini tampilannya:
contoh-program-mouse-event-java

Berikut ini program lengkapnya:
01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04
05public class MouseEventHandling extends JFrame
06    implements MouseListener, MouseMotionListener {
07     
08    private JLabel statusBar;
09     
10    public MouseEventHandling () {
11        super ("Mencoba Beberapa Mouse Event Handling");
12         
13        statusBar = new JLabel();
14        getContentPane().add(statusBar,BorderLayout.SOUTH);
15         
16        addMouseListener (this);
17        addMouseMotionListener (this);
18         
19        setSize (300,100);
20        setLocationRelativeTo(null);
21        setVisible(true);
22    }
23     
24    //MouseListener event handler
25    public void mouseClicked (MouseEvent e) {
26        statusBar.setText("Clicked at ["+ e.getX() + "," + e.getY() + "]");
27    }
28     
29    public void mousePressed (MouseEvent e) {
30        statusBar.setText("Pressed at ["+ e.getX() + "," + e.getY() + "]");
31    }
32
33    public void mouseReleased (MouseEvent e) {
34        statusBar.setText("Released at ["+ e.getX() + "," + e.getY() + "]");
35    }
36
37    public void mouseEntered (MouseEvent e) {
38        statusBar.setText("Entered at ["+ e.getX() + "," + e.getY() + "]");
39        getContentPane().setBackground(Color.GREEN);
40    }
41
42    public void mouseExited (MouseEvent e) {
43        statusBar.setText("Mouse outside window");
44        getContentPane().setBackground(Color.WHITE);
45    }
46     
47    //MouseMotionListener event handler
48    public void mouseDragged (MouseEvent e) {
49        statusBar.setText("Dragged at ["+ e.getX() + "," + e.getY() + "]");
50    }
51     
52    public void mouseMoved (MouseEvent e) {
53        statusBar.setText("Moved at ["+ e.getX() + "," + e.getY() + "]");
54    }
55     
56    public static void main (String args[]) {
57        MouseEventHandling test = new MouseEventHandling();
58        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59    }
60}

0 :

Posting Komentar

Related Posts Plugin for WordPress, Blogger...
 

Contoh Pilihan

SMS GRATIS

Like Box