我试图打印前十名的人物和他们的名字。
我的思想:
private JLabel JSingleplayer [];
private JLabel JSingleScore [];
private String [] PlayerName = {"----------","----------","----------","----------","----------","----------","----------","----------","----------","----------"};
private String [] PlayerScore = {"--","--","--","--","--","--","--","--","--","--",};
并尝试在此处打印:
for (int x = 0; x < 10; x++)
{
JSingleplayer[x].setText(Singleplayer[x]);
JSingleplayer[x] = new JLabel(Singleplayer[x]);
EndPanelscore.add(JSingleplayer[x]);
JSingleScore[x].setText(SingleScore[x]);
EndPanelscore.add(JSingleScore[x]);
}
EndFrame.add(EndPanelscore);
当我添加for循环时,我收到一个大错误,它不允许打印数组。
如果您有解决方案或更好的方法来编写该部分(正常工作)请帮助我编写它的方式。将评价为5,就像所有好东西一样。
答案 0 :(得分:0)
你有一些问题。
首先,在Java中,您需要在为数组赋值之前声明数组的长度。
其次,您的代码试图在将标签分配给数组之前设置标签的文本。
第三,你使用的是一个不存在的变量(Singleplayer
)。
尝试以下更改,看看你是否可以从那里开始......
private JLabel JSingleplayer [] = new JLabel[10];
for (int x = 0; x < 10; x++)
{
JSingleplayer[x] = new JLabel(PlayerName[x]);
// the following assumes you've also declared EndPanelscore...
EndPanelscore.add(JSingleplayer[x]);
// you have similar problems here...
//JSingleScore[x].setText(SingleScore[x]);
//EndPanelscore.add(JSingleScore[x]);
}