Java认为方法是静态的?
由于某种原因,当我尝试在操作中从另一个类调用非静态方法时,收到一条错误消息,指出我无法在静态方法中调用非静态方法。然而,我从未将动作或其父级定义为静态的。为什么我会收到此错误?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyListenerFrame extends JFrame implements KeyListener {
GridLayout gridLayout1 = new GridLayout();
JPanel listenerPan = new JPanel();
public KeyListenerFrame() {
try {
jbInit();
listenerPan.addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0,true), "showKey");
listenerPan.getActionMap().put("showKey", showKey);
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "showKey");
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
KeyListenerFrame klf = new KeyListenerFrame();
klf.setBounds(10,10,500,500);
klf.setVisible(true);
klf.focusPanel();
}
public void focusPanel() {
listenerPan.requestFocus();
}
private void jbInit() throws Exception {
this.getContentPane().add(listenerPan, null);
ColorPanel panel = new ColorPanel(Color.lightGray);
this.getContentPane().add(panel);
}
这是给我带来麻烦的部分
Action showKey = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
ColorPanel.moveLeft();
}
};
public void keyTyped(KeyEvent e) {
System.out.println(e.toString());
}
public void keyPressed(KeyEvent e) {
System.out.println(e.toString());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.toString());
}
}
ColorPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import java.awt.Container.*;
import javax.swing.JComponent.*;
public class ColorPanel extends JPanel{
public Circle circle;
private javax.swing.Timer timer;
public ColorPanel(Color backColor){
int width = 500;
int height = 500;
setBackground(backColor);
setPreferredSize(new Dimension(width, height));
circle = new Circle(width / 2, height / 2, 50, Color.red);
circle.setVelocity(5);
addMouseListener(new MoveListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
circle.fill(g);
}
public void moveLeft(){
circle.move();
repaint();
}
/* public class MoveListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
circle.move();
repaint();
}
}
private class MoveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
circle.move();
//circle.turn(1);
repaint();
}
} */
}
从 ColorPanel.moveLeft(); 生成的错误; is: KeyListenerFrame.java:51: 无法从静态上下文引用非静态方法 moveLeft() ColorPanel.moveLeft();
For some reason, when I try to call a non-static method from another class in an action, I get an error saying I can't call a non-static method in a static method. However, I never defined the action or its parents as static. Why do I get this error?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyListenerFrame extends JFrame implements KeyListener {
GridLayout gridLayout1 = new GridLayout();
JPanel listenerPan = new JPanel();
public KeyListenerFrame() {
try {
jbInit();
listenerPan.addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0,true), "showKey");
listenerPan.getActionMap().put("showKey", showKey);
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "showKey");
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
KeyListenerFrame klf = new KeyListenerFrame();
klf.setBounds(10,10,500,500);
klf.setVisible(true);
klf.focusPanel();
}
public void focusPanel() {
listenerPan.requestFocus();
}
private void jbInit() throws Exception {
this.getContentPane().add(listenerPan, null);
ColorPanel panel = new ColorPanel(Color.lightGray);
this.getContentPane().add(panel);
}
this is the part that gives me trouble
Action showKey = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
ColorPanel.moveLeft();
}
};
public void keyTyped(KeyEvent e) {
System.out.println(e.toString());
}
public void keyPressed(KeyEvent e) {
System.out.println(e.toString());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.toString());
}
}
ColorPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import java.awt.Container.*;
import javax.swing.JComponent.*;
public class ColorPanel extends JPanel{
public Circle circle;
private javax.swing.Timer timer;
public ColorPanel(Color backColor){
int width = 500;
int height = 500;
setBackground(backColor);
setPreferredSize(new Dimension(width, height));
circle = new Circle(width / 2, height / 2, 50, Color.red);
circle.setVelocity(5);
addMouseListener(new MoveListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
circle.fill(g);
}
public void moveLeft(){
circle.move();
repaint();
}
/* public class MoveListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
circle.move();
repaint();
}
}
private class MoveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
circle.move();
//circle.turn(1);
repaint();
}
} */
}
The error generated from the ColorPanel.moveLeft(); is: KeyListenerFrame.java:51: non-static method moveLeft() cannot be referenced from a static context
ColorPanel.moveLeft();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您到底想在这里做什么:
ColorPanel 是一个类,而它应该是一个实例。
您可能打算做的是(假设 showKey 是在 ColorPanel 中定义的):
编辑以添加以下内容:
ColorPanel 在此创建,但不保留引用。
如果您将 ColorPanel 面板移到 jbInit 之外,然后将其初始化,因为
您将获得参考。然后调用 panel.moveLeft() 而不是 ColorPanel.moveLeft()
What exactly are you trying to do here:
ColorPanel is a class, when it should be an instance.
What you may be intending to do is (assuming showKey is defined within ColorPanel):
Edit To Add the following:
ColorPanel is created in here, but a reference isn't kept.
If you move ColorPanel panel outside of jbInit, and then initialize it as
you'll have your reference. Then instead of ColorPanel.moveLeft() call panel.moveLeft()
问题是您试图像静态方法一样访问它。您需要创建该类的实例,然后通过该实例调用 moveLeft()。
还创建对 ColorPanel 对象的全局引用。这将是您调用 moveLeft() 的 ColorPanal 类的实例。
The problem is that you are trying to access it as you would if it were a static method. You need to create an instance of the class, then through the instance you call moveLeft().
Also create a global reference to your ColorPanel Object. This would be the instance of the ColorPanal class you call moveLeft() on.
不,你不需要。没有这样的消息,也没有这样的错误。您收到错误消息“无法从静态上下文引用非静态方法 method()”。
No you don't. There is no such message and no such error. You get an error saying 'non-static method method() cannot be referenced from a static context.'