Menaxhimi i ngjarjeve te mouse-it
• Nderfaqja MouseMotionListener
– mouseDragged() dhe mouseMoved()
• Percakton nese mouse po leviz ose po zhvendoset mbi nje komponent te caktuar
• Nderfaqja MouseListener
– mousePressed(), mouseClicked(), dhe mouseReleased()
• Metodat analoge te ngjarjeve te tastieres
– mouseEntered() dhe mouseExited()
• tregojne nese mouse ka hyre brenda nje komponenti ose ka dale jashte tij.
• Nderfaqja MouseInputListener
– Implementon te gjitha metodat ne nderfaqet MouseListener dhe MouseMotionListener
– Nuk ka metoda te vetat
– Menaxhon tipe te ndryshme ngjarjesh te mouse-it
• MouseEvent
– Tipi i ngjarjeve te gjeneruar nga manipulimi i mouse
– Permban metoda dhe fusha instance
• Te perdorshme per pershkrimin e ngjarjeve te gjeneruara nga mouse
Shembull:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JMouseActionFrame extends JFrame implements MouseListener
{
private int x, y;
private JLabel label= new JLabel("Do something with the mouse");
String msg = "";
public JMouseActionFrame()
{
setTitle("Mouse Actions");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
addMouseListener(this);
add(label);
}
public void mouseClicked(MouseEvent e)
{
int whichButton = e.getButton();
msg = "You pressed mouse ";
if(whichButton == MouseEvent.BUTTON1)
msg += "button 1.";
else
if(whichButton == MouseEvent.BUTTON2)
msg += "button 2.";
else
msg += "button 3.";
msg += " You are at position " +
e.getX() + ", " + e.getY() + ".";
if(e.getClickCount() == 2)
msg += " You double-clicked.";
else
msg += " You single-clicked.";
label.setText(msg);
}
public void mouseEntered(MouseEvent e)
{
msg = "You entered the frame.";
label.setText(msg);
}
public void mouseExited(MouseEvent e)
{
msg = "You exited the frame.";
label.setText(msg);
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public static void main(String[] args)
{
JMouseActionFrame mFrame = new JMouseActionFrame();
final int WIDTH = 750;
final int HEIGHT = 300;
mFrame.setSize(WIDTH, HEIGHT);
mFrame.setVisible(true);
}
}