static JLabel jLabel2 = new JLabel((Icon) new ImageIcon("2.png"));
static JLabel jLabel4 = new JLabel((Icon) new ImageIcon("4.png"));
static JLabel jLabel8 = new JLabel((Icon) new ImageIcon("8.png"));
static JLabel jLabel16 = new JLabel((Icon) new ImageIcon("16.png"));
static JLabel jLabel32 = new JLabel((Icon) new ImageIcon("32.png"));
static JLabel jLabel64 = new JLabel((Icon) new ImageIcon("64.png"));
static JLabel jLabel128 = new JLabel((Icon) new ImageIcon("128.png"));
static JLabel jLabel256 = new JLabel((Icon) new ImageIcon("256.png"));
static JLabel jLabel512 = new JLabel((Icon) new ImageIcon("512.png"));
static JLabel jLabel1024 = new JLabel((Icon) new ImageIcon("1024.png"));
static JLabel jLabel2048 = new JLabel((Icon) new ImageIcon("2048.png"));
static JLabel jLabel1 = new JLabel((Icon) new ImageIcon("1.png"));
static JLabel jLabel0 = new JLabel((Icon) new ImageIcon("bj.png")); static JLabel im = null;
private Panel panel1 = new Panel();
public static JLabel Image(int i) {
if (i == 2) {
im = jLabel2;
} else if (i == 4) {
im = jLabel4;
} else if (i == 8) {
im = jLabel8;
} else if (i == 16) {
im = jLabel16;
} else if (i == 32) {
im = jLabel32;
} else if (i == 64) {
im = jLabel64;
} else if (i == 128) {
im = jLabel128;
} else if (i == 256) {
im = jLabel256;
} else if (i == 512) {
im = jLabel512;
} else if (i == 1024) {
im = jLabel1024;
} else if (i == 2048) {
im = jLabel2048;
} else if (i == -1) {
// System.out.println("出现数字1");
im = jLabel1;
} else if (i == 0) {
im = jLabel0;
}
return im;
}
public text(int[][] array) {
panel1.setLayout(new GridLayout(4, 4));
JLabel image = null;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
image = Image(array[i][j]);
add(image);//此处应该是问题所在
//panel1.add(new Label(String.valueOf(array[i][j])));
}
}
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = c.gridy = 0;
add(panel1, c);
}
//在main中调用text(int[][] array);text(int[][] array)中调用 Image(int i);根据传入不同的数字,分别输出对应的图片。
如果用panel1.add(new Label(String.valueOf(array[i][j])));该语句的话,可以在界面中输出数字,可是用image = Image(array[i][j]); add(image); 。界面里什么都没有,求解答。。。。。谢谢
相关分类