How to add mouselistener to a frame

I want to add a mouselistener to the MT JFrame framework, but when I execute frame When addMouseListener (this), I get an error that I can't use in static methods

I'm creating an application to detect mouse clicks and display 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) {}
}

Solution

This does not exist in static methods, because static methods are methods of classes, not objects (the owner of this) Solution: get rid of all static in the above code Except for the main method, none of the above fields or methods should be static

As Andrew Thompson correctly pointed out, the editor adds mouselistener to the JPanel added to the contentpane of JFrame

Edit 2

>You will need to learn and use the Java Naming Convention Class names (i.e. numberofclicks) should start with uppercase letters Method and variable names with lowercase letters. > It is better to use the mousePressed (...) method instead of mouseclicked (...), because the former is not appropriate for accepting the printing press. > You also need to set the text of jtextfield in the mousePressed (...) method, because changing the click value alone is not enough to change the display. > I try to avoid having my GUI (or "view") class implement my listener I prefer to use anonymous inner classes or independent classes as much as possible

For example,

JPanel mainPanel = new JPanel();
  mainPanel.addMouseListener(new MouseAdapter() {
     @Override
     public void mousePressed(MouseEvent e) {
        clicks++;
        text.setText(clicks + " Clicks");
     }
  });
  // add mainPanel to the JFrame...
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>