Java认为方法是静态的?

发布于 2024-11-05 17:07:25 字数 3271 浏览 0 评论 0原文

由于某种原因,当我尝试在操作中从另一个类调用非静态方法时,收到一条错误消息,指出我无法在静态方法中调用非静态方法。然而,我从未将动作或其父级定义为静态的。为什么我会收到此错误?

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-11-12 17:07:25

您到底想在这里做什么:

   ColorPanel.moveLeft();

ColorPanel 是一个类,而它应该是一个实例。

您可能打算做的是(假设 showKey 是在 ColorPanel 中定义的):

   ColorPanel.this.moveLeft();

编辑以添加以下内容:

private void jbInit() throws Exception {
  this.getContentPane().add(listenerPan, null);
  ColorPanel panel = new ColorPanel(Color.lightGray);
  this.getContentPane().add(panel);
}

ColorPanel 在此创建,但不保留引用。

如果您将 ColorPanel 面板移到 jbInit 之外,然后将其初始化,因为

panel = new ColorPanel(Color.lightGray);

您将获得参考。然后调用 panel.moveLeft() 而不是 ColorPanel.moveLeft()

What exactly are you trying to do here:

   ColorPanel.moveLeft();

ColorPanel is a class, when it should be an instance.

What you may be intending to do is (assuming showKey is defined within ColorPanel):

   ColorPanel.this.moveLeft();

Edit To Add the following:

private void jbInit() throws Exception {
  this.getContentPane().add(listenerPan, null);
  ColorPanel panel = new ColorPanel(Color.lightGray);
  this.getContentPane().add(panel);
}

ColorPanel is created in here, but a reference isn't kept.

If you move ColorPanel panel outside of jbInit, and then initialize it as

panel = new ColorPanel(Color.lightGray);

you'll have your reference. Then instead of ColorPanel.moveLeft() call panel.moveLeft()

娜些时光,永不杰束 2024-11-12 17:07:25

问题是您试图像静态方法一样访问它。您需要创建该类的实例,然后通过该实例调用 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.

爱的十字路口 2024-11-12 17:07:25

我收到一条错误消息,提示我无法调用
静态方法中的非静态方法

不,你不需要。没有这样的消息,也没有这样的错误。您收到错误消息“无法从静态上下文引用非静态方法 method()”。

I get an error saying I can't call a
non-static method in a static 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.'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文