import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DemoMouseListener extends JFrame implements MouseListener
{
TextField tf=new TextField(60);
DemoMouseListener()
{
setTitle("Contoh Reaksi Mouse");
setLocation(200,100);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
}
void KomponenVisual()
{
getContentPane().setLayout(null);
getContentPane().add(tf);
tf.setBounds(10,10,250,20);
setVisible(true);
}
public void mouseClicked(MouseEvent me)
{
Graphics g= getGraphics();
int x=me.getX();
int y=me.getY();
g.drawString("posisi:"+x+","+y,x,y);
String msg="Mouse clicked";
tf.setText(msg);
}
public void mouseExited(MouseEvent me)
{
String msg="Mouse exited component" ;
tf.setText(msg);
}
public void mousePressed(MouseEvent me)
{
Graphics g= getGraphics();
int x=me.getX();
int y=me.getY();
g.drawOval(x-15,y-15,30,30);
String msg="Mouse Pressed";
tf.setText(msg);
}
public void mouseReleased(MouseEvent me)
{
Graphics g= getGraphics();
int x=me.getX();
int y=me.getY();
g.drawRect(x-15,y-15,30,30);
String msg="Mouse Released";
tf.setText(msg);
}
public static void main(String args[])
{
DemoMouseListener med=new DemoMouseListener();
med.KomponenVisual();
}}
No comments:
Write comments