在不同的 class 中调用方法并传递一个数组
Calling a method in a different class and passing an array
现在我不确定我是否刚刚把自己搞糊涂了。我在网上做了很多搜索,但似乎没有帮助,所以我想我会在这里问。我实际上是在尝试根据数组 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[]
}
这是另一个class:
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);
}
}
谢谢,请耐心等待我只是想学习:)
创建对象到网格 class
grid table1 = new grid();
并通过传递数组
对该对象调用游戏方法
table1.game(game);
现在我不确定我是否刚刚把自己搞糊涂了。我在网上做了很多搜索,但似乎没有帮助,所以我想我会在这里问。我实际上是在尝试根据数组 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[]
}
这是另一个class:
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);
}
}
谢谢,请耐心等待我只是想学习:)
创建对象到网格 class
grid table1 = new grid();
并通过传递数组
对该对象调用游戏方法table1.game(game);