修复静态方法问题



所以我对Java比较陌生,但我遇到了静态内容的问题。首先,我不确定静态的东西是什么,但我知道这很烦人,其次,我一直在练习这个"乒乓球"小游戏,我试图建立一个记分牌,但它说Cannot make a static reference to the non-static method getScore()

这是我下面的代码,任何建议都会有所帮助,因为我仍然是菜鸟。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PongGame extends JComponent implements ActionListener, MouseMotionListener{
    private int ballX = 385;
    private int ballY = 285;
    private int paddleOpX = 0;
    private int paddleX = 0; 
    private int ballYSpeed = 1;
    private int ballXSpeed = 1;
    public Integer score = 0;
    private static Timer t = null;
    public static void main(String[] args){
        JLabel scoreBoard = new JLabel(getScore().toString());
        JFrame window = new JFrame("Hit the Damn Ball");
        window.setLayout(new BorderLayout());
        window.getContentPane().setBackground(new Color(0, 0, 0));
        PongGame game = new PongGame();
        window.add(game);
        window.pack();
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        t = new Timer(5, game);
        t.start();
        window.addMouseMotionListener(game);
    }
    //set the size of window
    public Dimension getPreferredSize(){
        return new Dimension(800, 600);
    }
    @Override
    protected void paintComponent(Graphics g){
        g.setColor(new Color(110, 65, 13));
        g.fillRect(paddleX, 510, 150, 15);
        g.setColor(new Color(90, 0,0));
        g.fillRect(paddleOpX, 90, 150, 15);
        g.setColor(Color.WHITE);
        g.fillOval(ballX, ballY, 30, 30);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        paddleOpX = (ballX+5);
        ballX +=ballXSpeed;
        ballY +=ballYSpeed;
        if(ballX >= paddleX-30 && ballX <= (paddleX + 150) && ballY >= 480){
            ballYSpeed = -ballYSpeed;
            //ballYSpeed = -1;
            setScore();
        }
        if(ballX >= paddleX-30 && ballX <= (paddleX + 150) && ballY > 480){
            ballYSpeed = -ballYSpeed;
            //ballYSpeed = 1;
            setScore();
        }
        if(ballX >= paddleOpX-30 && ballX <=(paddleOpX + 150) && ballY <= 106){
            ballYSpeed = ballYSpeed*-1;
        }
        if(ballY > 570){
            ballXSpeed  = 0;
            ballYSpeed = 0;
            t.stop();
            System.out.println(score);
        }
        if(ballX >= 770){
            ballXSpeed = -ballXSpeed;
            //ballXSpeed = -1;
        }
        if(ballY <= 0){
            ballXSpeed  = 0;
            ballYSpeed = 0;
            t.stop();
            System.out.println(score);
            //ballYSpeed = 1;
        }
        if(ballX <= 0){
            ballXSpeed = ballXSpeed*-1;
            //ballXSpeed = 1;
        }
        repaint();
    }
    @Override
    public void mouseDragged(MouseEvent e) {
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        paddleX  = e.getX()-75;
        if(ballX >= paddleX-30 && ballX <= (paddleX + 150) && ballY == 480 ){
            ballYSpeed = ballYSpeed-1;
            ballXSpeed = ballXSpeed < 0 ? -2 : 2;
            //ballYSpeed = 1;
        }
        repaint();
    }
    private void setScore(){
        score++;
    }
    public Integer getScore(){
        return score;
    }
}

实例方法getScore()需要一个对象来调用该方法,因为它不是static,但你还没有。

PongGame game = new PongGame();移动到main的第一行,然后更改

JLabel scoreBoard = new JLabel(getScore().toString());

JLabel scoreBoard = new JLabel(game.getScore().toString());

private void setScore()方法不是static方法,并且您正在尝试从静态方法调用此方法,您必须将此方法设置为静态或使用对象调用此方法。

若要创建static方法,必须在方法语法中使用static修饰符。

private static void setScore(){
        score++;
    }

您必须创建PongGame类的实例,然后调用该方法。

PongGame pg = new PongGame();
JLabel scoreBoard = new JLabel(pg.getScore().toString());

方法getScore()不是static(因为你没有将其声明为 static ),所以它需要一个实例来调用。换句话说,您必须创建类PongGame的实例:

public static void main(String[] args){
    PongGame game = PongGame(...);
    ...
}

然后使用它来调用该方法:

game.getScore();

注意:static 方法每个类仅存在一次,因此您不需要该类的实例即可调用它。另一方面,非static方法需要调用实例。

getScore() 需要它是静态的,因为你没有输入"new",所以如果没有它的实例,它就无法计算。

 JLabel scoreBoard = new JLabel(getScore().toString());

需要它,并且您尚未将其声明为"静态"。

静态意味着它只一劳永逸地加载到内存中(这在某些情况下很好(例如在您的服务器上运行的 Web 方法(可能内存不足)))。

简单的解决方案:创建一个实例并使用它。

  getScore gs= new getScore()  

或将其设为静态并将其用作

  getScore.TosTring();

在此之前,您需要实例化超类。

最新更新