2014-04-16 136 views
-1

因此,我继续并像往常一样开始创建游戏。除了这一次,我得到了一个错误O.o.我试图找到答案,但非我认为也许在这里我可以得到一个很好的答案!继承人的错误:Java - 无法对类型为“东西”的非静态方法“东西”进行静态引用

Description Resource Path Location Type Cannot make a static reference to the non-static field image Game.java /POGA/src/packagehere line 71 Java Problem

Description Resource Path Location Type Cannot make a static reference to the non-static method createBufferStrategy(int) from the type Canvas Game.java /POGA/src/packagehere line 66 Java Problem

I get more of these type, but I guess If you show me and viewers of this question how to fix these errors we would be able to fix the rest of the "same" ones...

package packagehere; 

import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 

public class Game extends Canvas implements Runnable{ 
private static final long serialVersionUID = 1L; 

BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 
public static boolean running = false; 
JFrame frame; 
public static String title = "POGA game thingy - 0.1 Aplha"; 
public static final int WIDTH = 800; 
public static final int HEIGHT = 600; 
public static final Dimension gameDim = new Dimension(WIDTH, HEIGHT); 

synchronized void start() { 
    Thread thread = new Thread(); 
    thread.start(); 
    running = true; 
} 

public void run() { 
    while(running) { 
     tick(); 
     render(); 
    } 
} 

synchronized void stop() { 
    running = false; 
    System.exit(0); 
} 

public Game() { 
    setMaximumSize(gameDim); 
    setMinimumSize(gameDim); 
    setPreferredSize(gameDim); 

    frame = new JFrame(title); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new BorderLayout()); 
    frame.add(this, BorderLayout.CENTER); 
    frame.pack(); 

    frame.setLocationRelativeTo(null); 
    frame.setResizable(false); 
    frame.setVisible(true); 

    frame.requestFocus(); 
} 

public static void tick() { 

} 

public static void render() { 
    BufferStrategy bs = getBufferStrategy(); 
    if(bs == null) { 
     bs = createBufferStrategy(3); 
     return; 
    } 
    Graphics g = bs.getDrawGraphics(); 

    g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 

    g.dispose(); 
    bs.show(); 
} 
} 
+0

可能重复(http://stackoverflow.com/questions/8101585/cannot-make-a-static-reference-to-the-non -static-field) –

+0

为什么你有这么多标记为“静态”的东西?这看起来不正确。如果你想说'Game game = new Game()',然后在'game'上调用方法,请不要使用static。 –

+0

错误消息说明了这一切......您无法从静态块/方法中引用非静态字段。如果静态修饰符包含在字段或方法声明中,则不需要使用该类的任何实例。该字段或方法与类相关联,而不是单个对象。另外看看http://stackoverflow.com/questions/17622088/when-to-use-static-method-and-field –

回答

0

的问题是在低于行

g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 

什么对象是你叫getWidth()getHeight()方法二?

我想你想得到Canvas的宽度和高度。

To solve this issue remove static from render() method.

的[不能使静态参考非静态字段]
0

哪里getBufferStrategy()createBufferStrategy()申报? (答:在您的扩展型Canvas

它们是非静态方法,从render()方法中你不能访问它们,因为该方法是静态的。

您可以从静态和非静态方法中访问静态方法。 只能从非静态方法访问非静态方法。

+0

@Braj是的,因为这是错误:'不能对非静态方法createBufferStrategy(int)进行静态引用'(第一个问题是修辞) – dognose

相关问题