尝试创建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);
答案 0 :(得分:1)
如果正确使用,请尝试检查图像路径。或者,如果您可以真正使图像出现,也可以进行测试,尝试使其成为绝对路径。而且,请在那里详细说明你的问题。
答案 1 :(得分:1)
正如ben75所写,它需要frame.pack(); 对他的信任!
答案 2 :(得分: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);