Java MouseEvent.getButton()
Syntax
MouseEvent.getButton() has the following syntax.
public int getButton()
Example
In the following code shows how to use MouseEvent.getButton() method.
/*w w w .j a v a 2 s. c om*/
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
JButton button = new JButton("I Move");
public Main() {
setLayout(null);
add(button);
button.setSize(button.getPreferredSize());
button.setLocation(20, 20);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println(e.getButton() == MouseEvent.BUTTON1);
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("MoveButton");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(250, 200);
frame.setLocation(200, 200);
frame.setContentPane(new Main());
frame.setVisible(true);
}
}
Home »
Java Tutorial »
java.awt.event »
Java Tutorial »
java.awt.event »