【问题标题】:Have a function return a JLabel and add it to a JFrame让函数返回 JLabel 并将其添加到 JFrame
【发布时间】: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


【解决方案1】:
1) realSim2.add(Planet.createPlanet(1));
2) Planet.createPlanet(1).setBounds(100, 100, 20, 20);
3) Planet.createPlanet(1).setText("Ello, just testing!");

在第一行。您向 JPanel 添加一个新标签。但该标签没有任何文字。因为它只是由函数创建的,所以它也没有任何大小。

在第二行创建一个新的 JLabel 并设置一个大小,仅此而已。您不会将其添加到面板中。

第三行你做的和第二行一样。创建一个新的 JLabel,但您没有添加它。

试试这个代码:

JLabel label = Planet.createPlanet(1);
label.setBounds(100, 100, 20, 20);
label.setText("Ello, just testing!");
realSim2.add(label); //rememebr to add the object to the panel

【讨论】:

  • 啊,谢谢。那解决了它。这是一个愚蠢的错误。
【解决方案2】:
        realSim2.add(Planet.createPlanet(1)); 
     //above line you have added label to frame
     //after that, lines below will not have any effect on the one that 
     // is already added. 
        Planet.createPlanet(1).setBounds(100, 100, 20, 20);
        Planet.createPlanet(1).setText("Ello, just testing!");

所以最后两行代码只会创建新的 Jlabel 对象并且是垃圾,因为您在代码中没有引用这些对象。

【讨论】:

    【解决方案3】:

    您正在设置新创建的JLabel 的边界和文本,而不是您首先创建的。所以,应该是:

    JLabel planet = Planet.createPlanet(1)
    planet.setBounds(100, 100, 20, 20);
    planet.setText("Ello, just testing!");
    realSim2.add(planet);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多