gpt4 book ai didi

java - 调用不同类中的方法并传递数组

转载 作者:行者123 更新时间:2023-12-01 11:58:18 26 4
gpt4 key购买 nike

现在我不确定我是否在这里感到非常困惑。我在网上做了很多搜索,但这似乎没有帮助,所以我想我会在这里问。我本质上是试图根据数组 game[] 中的值创建一个彩色网格。

public class first
{
public static void Newgame(){
//variables etc
game = new int[100];

for(int i=0; i<100; i++)
{
game[i] = 0;
if(i==89){
game[i] = 2;
}
}

grid table1 = new grid(game[i]); // I'm trying to call the method in the other class and create the jframe grid using the values from the array game[]
}

这是另一个类:

public class grid extends JFrame {
void game(int[] value) {
setSize(400, 400);
int size = 10;


JPanel content = new JPanel(new GridLayout(size,size));

for (int i = 0; i < size*size; ++i) {
JPanel panel = new JPanel();
if(value[i]==0){
panel.setBackground(Color.BLACK);
}
if(value[i]==1){
panel.setBackground(Color.RED);
}
if(value[i]==2){
panel.setBackground(Color.GREEN);
}

content.add(panel);
}
add(content);
setVisible(true);
}

}

谢谢,请耐心等待,我只是想学习:)

最佳答案

创建网格类对象

grid table1 = new grid();

并通过传递数组调用该对象上的游戏方法

table1.game(game);

关于java - 调用不同类中的方法并传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com