【发布时间】:2015-01-30 23:14:33
【问题描述】:
我编写了一个返回 JLabel 的函数,另一个函数将它添加到 JFrame,但是,似乎没有向其中添加 JLabel。我在 JLabel 上测试了不同的东西,例如颜色和文本,但没有显示出来。我通常在 JFrame 中添加了一个 JLabel,这很有效。我真的很希望能够将我的函数添加到 JFrame 中。
我有一个创建新框架的 JButton,代码如下:
inputButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFrame realSim = new JFrame();
JPanel realSim2 = new JPanel();
newFrame(realSim, realSim2);
realSim2.add(Planet.createPlanet(1));
Planet.createPlanet(1).setBounds(100, 100, 20, 20);
Planet.createPlanet(1).setText("Ello, just testing!");
}
}
);
Planet.createPlanet() 是返回 JLabel 的函数。 这是该函数的代码:
public static JLabel createPlanet(int planetNum)
{
JLabel planetRep = new JLabel();
switch (planetNum)
{
case 1: planetColor = Color.WHITE;
break;
case 2: planetColor = Color.RED;
break;
case 3: planetColor = Color.ORANGE;
break;
case 4: planetColor = Color.YELLOW;
break;
case 5: planetColor = Color.GREEN;
break;
case 6: planetColor = Color.CYAN;
break;
case 7: planetColor = Color.BLUE;
break;
case 8: planetColor = Color.MAGENTA;
break;
case 9: planetColor = Color.PINK;
break;
default: planetColor = Color.BLACK;
break;
}
planetRep.setBackground(planetColor);
planetRep.setOpaque(true);
return planetRep;
}
我想不出我可能做错了什么。任何帮助将不胜感激。
【问题讨论】:
-
planetRep 有文字吗?它可能太小而看不见
标签: java function jframe jlabel