所以我对Java比较陌生,但我遇到了静态内容的问题。一个我不确定什么是静态的东西,但我知道这是非常讨厌的,其次,我有这个小小的游戏" Pong"我一直在努力锻炼,我试图让记分牌上升,但它是在说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;
}
}
答案 0 :(得分:1)
实例方法getScore()
需要一个对象来调用该方法,因为它不是static
,但你还没有。{/ p>
将PongGame game = new PongGame();
移至main
的第一行,然后更改
JLabel scoreBoard = new JLabel(getScore().toString());
到
JLabel scoreBoard = new JLabel(game.getScore().toString());
答案 1 :(得分:0)
private void setScore()
方法不是static
方法,并且您正在尝试从静态方法调用此方法,您必须将此方法设置为静态或使用object调用此方法。强>
要创建static
方法,您必须在方法语法中使用static
修饰符。
private static void setScore(){
score++;
}
OR
你必须创建PongGame
类的实例,然后调用该方法。
PongGame pg = new PongGame();
JLabel scoreBoard = new JLabel(pg.getScore().toString());
答案 2 :(得分:0)
getScore()需要它是静态的,因为你没有放置&#34; new&#34;,所以如果没有它的实例,它就无法计算。
JLabel scoreBoard = new JLabel(getScore().toString());
需要它并且您没有将其声明为&#34;静态&#34;。
静态意味着它只加载到内存一次 (这对某些情况很有用(比如在服务器上运行的Web方法(可能内存不足))。)
简单的解决方案:创建一个实例并使用它。
getScore gs= new getScore()
或将其设为静态并将其用作
getScore.TosTring();
您需要在此之前实例化超类。
答案 3 :(得分:0)
方法getScore()
不是static
(因为您没有将其声明为static
),因此需要调用实例。换句话说,您必须创建类PongGame
的实例:
public static void main(String[] args){
PongGame game = PongGame(...);
...
}
然后用它来调用方法:
game.getScore();
注意:每个类只存在一个static
方法,因此您不需要该类的实例来调用它。另一方面,非static
方法需要调用实例。