Java认为方法是静态的?

时间:2011-05-04 17:59:51

标签: java methods static non-static

出于某种原因,当我尝试从一个动作中的另一个类调用非静态方法时,我得到一个错误,说我不能在静态方法中调用非静态方法。但是,我从未将动作或其父母定义为静态。为什么我会收到此错误?

    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()生成的错误;是:KeyListenerFrame.java:51:无法从静态上下文引用非静态方法moveLeft()     ColorPanel.moveLeft();

3 个答案:

答案 0 :(得分:8)

你到底想做什么:

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

你会得到你的参考。然后代替ColorPanel.moveLeft()调用panel.moveLeft()

答案 1 :(得分:0)

问题在于您尝试访问它,就像它是静态方法一样。您需要创建该类的实例,然后通过您调用moveLeft()的实例。

还创建对ColorPanel对象的全局引用。这将是您调用moveLeft()的ColorPanal类的实例。

答案 2 :(得分:0)

  

我收到一条错误消息,说我无法拨打电话   静态方法中的非静态方法

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