Tutorial Belajar Pemrograman, membuat game, membuat aplikasi, membuat program, android, game maker, yii, php, CSS, HTML, java, javascript, codeigniter, jquery, Pascal, c++

Friday, January 4, 2013

Contoh Program Java Swing Mouse Event Handling

Contoh Program Java Swing Mouse Event Handling - Kali ini kami akan memberi contoh sederhana program Java Oriented Object Programming. Program Mouse Event Handling ini akan meunjukan koordinat dari mouse atau pointer yang kita letakan di atas layar program yang kita jalankan. untuk lebih jelasnya lihat scriptnya di bawah :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mouse;

/**
 *
 * @author Jin Toples
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Mouse extends JFrame 
 implements MouseListener, MouseMotionListener {
 
 private JLabel statusBar;
 
 public Mouse () {
  super ("Mencoba Beberapa Mouse Event Handling");
  
  statusBar = new JLabel();
  getContentPane().add(statusBar,BorderLayout.SOUTH);
  
  addMouseListener (this);
  addMouseMotionListener (this);
  
  setSize (300,100);
  setLocationRelativeTo(null);
  setVisible(true);
 }
 
 //MouseListener event handler
 public void mouseClicked (MouseEvent e) {
  statusBar.setText("Clicked at ["+ e.getX() + "," + e.getY() + "]");
 }
 
 public void mousePressed (MouseEvent e) {
  statusBar.setText("Pressed at ["+ e.getX() + "," + e.getY() + "]");
 }

 public void mouseReleased (MouseEvent e) {
  statusBar.setText("Released at ["+ e.getX() + "," + e.getY() + "]");
 }

 public void mouseEntered (MouseEvent e) {
  statusBar.setText("Entered at ["+ e.getX() + "," + e.getY() + "]");
  getContentPane().setBackground(Color.GREEN);
 }

 public void mouseExited (MouseEvent e) {
  statusBar.setText("Mouse outside window");
  getContentPane().setBackground(Color.WHITE);
 }
 
 //MouseMotionListener event handler
 public void mouseDragged (MouseEvent e) {
  statusBar.setText("Dragged at ["+ e.getX() + "," + e.getY() + "]");
 }
 
 public void mouseMoved (MouseEvent e) {
  statusBar.setText("Moved at ["+ e.getX() + "," + e.getY() + "]");
 }
 
 public static void main (String args[]) {
     Mouse test = new Mouse();
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Coba jalankan script di atas, maka hasilnya akan seperti gambar di awal.

Baca juga : Program Menu Sederhana dengan Java Swing

Program Java Swing Mouse Event Handling anda sekarang sudah selsai, baca juga tutorial java swing lainnya.

Terima Kasih Telah Mengunjungi Blog Sederhana Ini.

Di Mohon Apabila Anda Ingin Mengcopas Artikel Pada Blog ini Cantumkan URL Sumber.

Sebagai Pengunjung Yang Baik Anda Dapat Meninggalkan Komentar di Blog Sederhana Ini.

Share this post

0 komentar

:) :) :-) :-) :)) :)) =)) =)) :( :( :-( :-( :(( :(( :d :d :-d :-d @-) @-) :p :p :o :o :>) :>) (o) (o) [-( [-( :-? :-? (p) (p) :-s :-s (m) (m) 8-) 8-) :-t :-t :-b :-b b-( b-( :-# :-# =p~ =p~ :-$ :-$ (b) (b) (f) (f) x-) x-) (k) (k) (h) (h) (c) (c) cheer cheer

 
© Jin Toples Programming
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0