Program:
Read: Create your own website
import java.awt.*;
import java.awt.event.*;
import java.swing.*;
public class MouseEvents extends JFrame implements MouseListener,MouseMotionListener
{import java.awt.event.*;
import java.swing.*;
public class MouseEvents extends JFrame implements MouseListener,MouseMotionListener
string str=" ";
JTextArea ta;
container c;
int x,y;
MouseEvents()
{
c=getContentPane();
c.set Layout(new FlowLayout());
ta=new JTextArea("click the mouse or move it",5,20);
ta.set Font(new Font("Arial",Font.BOLD,30));
c.add(ta);
ta.addMouseListener(this);
ta.addMouseMotionListener(this);
}
public void mouseclicked(MouseEvent me)
{
int i=me.getButton();
if(i==1);
str+="clicked Button:left";
else if(i==2);
str+="clicked Button:middle";
if(i==3);
str+="clicked Button:Right";
this.didplay();
}
public void mouseEntered(MouseEvent me)
{
str+="mouse Entered";
this.display();
}
public void mouseEntered(MouseEvent me)
{
str+="mouse Exited";
this.display();
}
public void mousePressed(MouseEvent me)
{
x=me.get X();
y=me.get y();
str+="mouse pressed at:"+x+"\t"+y;
this.display();
}
public void mouseReleased(MouseEvent me)
{
x=me.get X();
y=me.get y();
str+="mouse Released at:"+x+"\t"+y;
this.display();
}
public void mouseDragged(MouseEvent me)
{
x=me.get X();
y=me.get y();
str+="mouse Dragged at:"+x+"\t"+y;
this.display();
}
public void mouseMoved(MouseEvent me)
{
x=me.get X();
y=me.get y();
str+="mouse Moved at:"+x+"\t"+y;
this.display();
}
public void display()
{
ta.setText(str);
str=" ";
}
public static void main(String args[])
{
MouseEvents mes=new MouseEvents();
mes.setSize(400,400);
mes.setVisible(true);
mes.setDefaultCloseOperation(JFrame.EXIT-ON-CLOSE);
}
}
No comments:
Post a Comment