MouseEvent Class
静的メソッドで、を使用してみてくださいMOUSE_CLICKED
詳細については、こちらをご覧ください。
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseEvent.html
roseindiaのこの例を参照してください
import java.awt.*;
import java.awt.event.*;
public class MouseClick {
Label lbl;
public static void main(String[] args) {
MouseClick MC = new MouseClick();
}
public MouseClick(){
Frame f = new Frame("Checking the mouse click");
Panel p = new Panel();
Button button = new Button("Click Me");
button.addMouseListener(new MyMouseListener());
p.add(button, BorderLayout.NORTH);
f.add(p,BorderLayout.NORTH);
lbl = new Label("Roseindia.net");
f.add(lbl, BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
f.setSize(400,400);
f.setVisible(true);
}
public class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent me){
String str = lbl.getText();
if (str.equals("Roseindia.net")){
lbl.setText("You have clicke the button.");
}
else if (str.equals("You have clicke the button.")){
lbl.setText("Roseindia.net");
}
}
}
}
Oracleのこの例を参照してください
public class MouseEventDemo ... implements MouseListener {
//where initialization occurs:
//Register for mouse events on blankArea and the panel.
blankArea.addMouseListener(this);
addMouseListener(this);
...
public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseReleased(MouseEvent e) {
saySomething("Mouse released; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseEntered(MouseEvent e) {
saySomething("Mouse entered", e);
}
public void mouseExited(MouseEvent e) {
saySomething("Mouse exited", e);
}
public void mouseClicked(MouseEvent e) {
saySomething("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
void saySomething(String eventDescription, MouseEvent e) {
textArea.append(eventDescription + " detected on "
+ e.getComponent().getClass().getName()
+ "." + newline);
}
}