2015-01-07 52 views
1

所以我對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; 
    } 
} 

回答

1

getScore()需要在其上調用該方法,因爲它不是static一個對象,但你還沒有一個實例方法。

移動PongGame game = new PongGame();main第一線,然後改變

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

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

好吧我做到了這一點,它的工作原理,但現在標籤不會顯示在屏幕上 –

+0

您需要'JLabel'添加'JFrame'的內容窗格。 – rgettman

+0

真棒我認爲這只是一個文本顏色問題,現在謝謝 –

0

private void setScore()方法不是static方法和你正試圖從靜態方法調用此方法,你必須要麼使該方法爲靜態或使用對象調用此方法。

要創建static方法,您必須在方法語法中使用static修飾符。

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

OR

你必須創建PongGame類的實例,然後調用該方法。

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

getScore()需要它是靜態的,因爲你沒有把 「新」,所以沒有它的實例,它不能計算。

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

需要它,你沒有聲明它是「靜態的」。

靜態意味着它只加載到內存一次,並且全部加載到 (這對於某些情況(如服務器上運行的Web方法(可能具有低內存))很有用))。

簡單的解決方案:創建一個實例並使用它。

getScore gs= new getScore() 

或使其靜態的,並用它作爲

getScore.TosTring(); 

您需要在此之前實例化超類。

0

方法getScore()不是static(因爲你沒有聲明它爲static),所以它需要一個實例來調用。換句話說,你必須創建類PongGame的一個實例:

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

,然後用它來調用方法:每類一個static方法只存在一次:

game.getScore(); 

注,所以你不需要一個類的實例來調用它。另一方面,非static方法需要調用一個實例。