Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- public class AllMouseEvents {
- public static void main(String args[]) {
- SwingUtilities.invokeLater(() -> {
- JFrame frame = new JFrame("MouseClickEvent Demo");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel panel = new JPanel();
- JLabel j1 = new JLabel();
- panel.add(j1);
- panel.setPreferredSize(new Dimension(300, 200));
- panel.addMouseListener(
- new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- JOptionPane.showMessageDialog(
- frame,
- "Mouse Clicked at(" + e.getX() + "," + e.getY() + ")"
- );
- }
- }
- );
- panel.addMouseMotionListener(
- new MouseAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- j1.setText("Mouse Moved at(" + e.getX() + "," + e.getY() + ")");
- }
- }
- );
- panel.addMouseListener(
- new MouseAdapter() {
- @Override
- public void mouseEntered(MouseEvent e) {
- frame.setTitle("Mouse Entered");
- }
- @Override
- public void mouseExited(MouseEvent e) {
- frame.setTitle("Mouse Exited");
- }
- }
- );
- frame.add(panel);
- frame.pack();
- frame.setVisible(true);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement