Tanp 2013-09-16 06:39 采纳率: 0%
浏览 2203

错误提示:non-static variable aa cannot be referenced from a static context

Java贪吃蛇,请大虾指教啊,我花较长时间缩减代码,如有什么还觉得不清楚,我的QQ172147501,谢谢了,实在不懂了。。。。。。。。。。。。。。。。。

package javaprogram;
import。。。。略-----第一个类:用于定义游戏的格子,在第三个类里共添加1600个

public class GameButton extends JButton{
public int flag;//1为身体 0为格子 -1为食物
public JButton fg;//游戏格子
public GameButton(){
this.flag=0;//格子
fg=new JButton();
}
}

package javaprogram;
import 。。。略-----第二个类:用于存放蛇的身体(身体由第一个类组成)、身体格子的每个x,y坐标

public class Bodyarrary extends JButton{
public int s_x;//蛇身体格子的x坐标

public int s_y;//蛇身体格子的y坐标

public GameButton bodyarrary=new GameButton();//用于存放蛇的身体

//一下代码有无均错
//public Bodyarrary(){

//    bodyarrary=new GameButton();
//}
public final void setpox(int x,int y,GameButton gb){
    this.s_x=x;
    this.s_y=y;
    //bodyarrary=new GameButton();
    this.bodyarrary=gb;
}

}

package javaprogram;
import 。。。。略----第三个类:定义游戏框架
class MyFrame extends JFrame implements ActionListener,KeyListener{//,MouseListener{
private JPanel gamepanel=new JPanel();//格子面板-----添加蛇的活动区域(由1600个类一对象组成
private JPanel buttonpanel=new JPanel();//按钮面板----添加开始、暂停按钮

private JButton start=new JButton("开始");//开始按钮
private JButton pause=new JButton("暂停");//暂停按钮   

private GameButton[][]fg;//存储1600个格子
private Bodyarrary[] bodyarrary=new Bodyarrary[100];//用于存放蛇的身体,最长为100
private static int body_length=0;

public MyFrame(String name){
super(name);
initgui();
}
public void initgui(){
BorderLayout blayout=new BorderLayout();
this.setLayout(blayout);//关联布局管理器

//添加两个面板
this.add(gamepanel,blayout.CENTER);//格子面板
this.add(buttonpanel,blayout.NORTH);//按钮面板

    GridLayout glayout=new GridLayout(40,40);//为gamepanel面板设置GridLayout()布局管理器
    gamepanel.setLayout(glayout);
    gamepanel.setSize(400,400);//设置gamepanel面板的大小
    addplates(gamepanel);//这个函数用于添加1600个格子
    gamepanel.setBorder(new LineBorder(new Color(102,102,255),2,true));

    buttonpanel.setLayout(new FlowLayout());//为buttonpanel面板设置FlowLayout()布局管理器
    buttonpanel.add(start);
    buttonpanel.add(pause);


    setResizable(false);

    start.addActionListener(this);
    pause.addActionListener(this);
    this.addKeyListener(this);   

    }

public void addplates(JPanel gamepanel){
fg=new GameButton[40][40];//40行40列
for(int i=0;i<40;i++){
for(int j=0;j<40;j++){
fg[i][j]=new GameButton();
fg[i][j].setSize(10,10);
fg[i][j].setIcon(new ImageIcon("E:\bg.jpg"));
gamepanel.add(fg[i][j]);
}
}
}

//以下函数初始化蛇,长度为5,

public void initsnake(){
     for(int i=0;i<=4;i++){
         fg[10][10+i].setIcon(new ImageIcon("E:\\body.jpg"));//改变原来的按钮图标,且为body.jpg
         fg[10][10+i].flag=1;//标注为在该位置有蛇的身体   *1身体        0背景格子      -1食物*



         //问题就是这行代码,有了后蛇的身体就只能初始化一格
         bodyarrary[body_length++].get_body_state(10,10+i,fg[10][10+i]);//存储初始化身体格子的x,y及身体

     }

//main入口类

public class JavaProgram{
public static void main(String[] args) {
MyFrame myframe=new MyFrame("TP 的Java程序......");
myframe.setVisible(true);
myframe.setSize(600,500);
myframe.initsnake();//添加初始化蛇函数
myframe.initfood();//添加初始化实物类
myframe.setFocusable(true);
}
}

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-02-13 14:28
    关注

    该回答引用ChatGPT

    这个错误提示是因为类MyFrame中的变量body_length声明为了static,但是bodyarrary数组并不是static的。在一个static context中,只能引用static的变量或方法。如果你希望使用bodyarrary数组,可以将body_length变量去掉static声明。


    代码如下:

    private Bodyarrary[] bodyarrary=new Bodyarrary[100];//用于存放蛇的身体,最长为100
    private int body_length=0;
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?