开发者

Java thinks a method is static?

开发者 https://www.devze.com 2023-03-02 16:36 出处:网络
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

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();
    }

    /*开发者_Go百科 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();


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()


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.


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.'

0

精彩评论

暂无评论...
验证码 换一张
取 消