如何将MouseListener添加到框架 [英] How To Add A MouseListener To A Frame
问题描述
我想在mt JFrame框架中添加一个mouselistener,但是当我执行frame.addMouseListener(this)时,我得到一个错误,我不能在静态方法中使用它
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
我正在创建一个检测鼠标点击的应用程序,然后在int点击中显示它
I am making an application that detects a click of the mouse then displays it in int clicks
代码
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) {}
}
推荐答案
此
在静态中不存在方法,因为静态方法是类的方法,而不是对象的方法( this
的所有者)。解决方案:摆脱上面代码中的所有静态。除主方法外,上述任何字段或方法都不应是静态的。
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.
编辑
正如Andrew Thompson正确说明的那样,添加将MouseListener添加到添加到JFrame的contentPane的JPanel。
Edit
And as Andrew Thompson correctly states, add the MouseListener to a JPanel that is added to the JFrame's contentPane.
编辑2
- 您将需要学习和使用Java命名约定。类名(即 N umberOfClicks)应以大写字母开头。带小写字母的方法和变量名。
- 最好使用
mousePressed(...)
方法而不是mouseClicked(...)
因为前者对接受印刷机的态度较少。 - 您还需要设置JTextField的文本在您的
mousePressed(...)
方法中,因为仅更改点击次数值不足以更改显示。 - 我尝试避免让我的GUI(或视图)类实现我的监听器。我更喜欢在可能的情况下使用匿名内部类或独立类。
- 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 themouseClicked(...)
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.
例如,
JPanel mainPanel = new JPanel();
mainPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
clicks++;
text.setText(clicks + " Clicks");
}
});
// add mainPanel to the JFrame...
这篇关于如何将MouseListener添加到框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!