Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Add A MouseListener To A Frame

I want to add a mouselistener to mt JFrame frame but when i do frame.addMouseListener(this) i get an error that i cannot use this in a static method

I am making an application that detects a click of the mouse then displays it in int clicks

code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class numberOfClicks implements MouseListener{

    static int clicks = 0;

    @Override
    public void mouseClicked(MouseEvent e) {
        clicks++;
    }

    static JTextField text = new JTextField();
    static String string = clicks+" Clicks";

    static JFrame frame = new JFrame("Click Counter");
    public static void frame(){
        Font f = new Font("Engravers MT", Font.BOLD, 23);
        text.setEditable(false);
        text.setBackground(Color.BLUE);
        text.setFont(f);
        text.setForeground(Color.GREEN);
        text.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        text.setText(string);

        frame.add(text, BorderLayout.SOUTH);
        frame.setResizable(false);
        frame.setSize(300, 300);
        frame.getContentPane().setBackground(Color.BLUE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.addMouseListener(this);
    }

    public static void main(String[] args){
        frame();
    }

    @Override
    public void mousePressed(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}
    @Override
    public void mouseReleased(MouseEvent e) {}
}
like image 254
ndfkj asdkfh Avatar asked Jun 24 '13 02:06

ndfkj asdkfh


People also ask

How do you use MouseListener?

Press and hold the mouse button again, and then drag the mouse so that the cursor ends up outside the window. Release the mouse button. You will see a mouse-pressed event, followed by a mouse-exited event, followed by a mouse-released event. You are not notified of the cursor's motion.

How do I add MouseListener to JLabel?

You can try : JLabel nameLabel = new JLabel("Name:"); nameLabel. addMouseMotionListener(new MouseMotionAdapter() { //override the method public void mouseDragged(MouseEvent arg0) { // to do ......................... } }); thats the way I understand your question.

What is the difference between MouseAdapter and MouseListener?

MouseListener is an Interface and MouseAdapter is an implementation of that. You can use the MouseAdapter in every place that you use a MouseListener.

What is a MouseListener?

MouseListener and MouseMotionListener is an interface in java.awt.event package . Mouse events. are of two types. MouseListener handles the events when the mouse is not in motion.


1 Answers

this doesn't exist in a static method since a static method is a method of the class, not of the object (the owner of this). Solution: get rid of all statics from your code above. None of your fields or methods above should be static other than the main method.


Edit
And as Andrew Thompson correctly states, add the MouseListener to a JPanel that is added to the JFrame's contentPane.


Edit 2

  • You will want to learn and use Java naming conventions. Class names (i.e., NumberOfClicks) should start with an upper case letter. Method and variable names with a lower-case letter.
  • You are better off using the mousePressed(...) method rather than the mouseClicked(...) since the former is less persnickety about accepting presses.
  • You will also want to set your JTextField's text in your mousePressed(...) method since just changing the clicks value isn't enough to change the display.
  • I try to avoid having my GUI (or "view") classes implement my listeners. I prefer to use anonymous inner classes or stand-alone classes where possible.

e.g.,

  JPanel mainPanel = new JPanel();
  mainPanel.addMouseListener(new MouseAdapter() {
     @Override
     public void mousePressed(MouseEvent e) {
        clicks++;
        text.setText(clicks + " Clicks");
     }
  });
  // add mainPanel to the JFrame...
like image 115
Hovercraft Full Of Eels Avatar answered Sep 23 '22 21:09

Hovercraft Full Of Eels