1

尝试创建一个 gridLayout 并用图像填充它,哪里出错了?

ImageIcon grassIcon = new ImageIcon("images/grass_tile.jpg"); 
JPanel panel = new JPanel(new GridLayout(haps,snaps,0,0));

JLabel labels[] = new JLabel[(haps*snaps)];

for (int i =  0; i < haps*snaps; i++)
{
    labels[i] = new JLabel(grassIcon);
    panel.add(labels[i]);
}

frame.add(panel);
4

3 回答 3

1

使用frame.setVisible(true); 显示框架;

ImageIcon grassIcon = new ImageIcon("images/grass_tile.jpg"); 
JPanel panel = new JPanel(new GridLayout(haps,snaps,0,0));

JLabel labels[] = new JLabel[(haps*snaps)];

for (int i =  0; i < haps*snaps; i++)
{
      labels[i] = new JLabel(grassIcon );
      panel.add(labels[i]);
}

frame.add(panel);
frame.pack();
frame.setVisible(true);
于 2013-01-28T09:48:10.457 回答
1

如果正确,请尝试检查您的图像路径。或者,如果您真的可以使图像出现,则可以进行测试,尝试使其成为绝对路径。另外,请详细说明您的问题。

于 2013-01-28T09:34:16.090 回答
1

正如 ben75 所写,它需要 frame.pack(); 对他的信任!

于 2013-01-28T09:34:30.753 回答