【问题标题】:Dynamic JPanel with objects not displaying不显示对象的动态 JPanel
【发布时间】:2011-05-07 22:31:00
【问题描述】:

有人可以看看我的这部分代码并告诉我为什么它不会返回 JPanel 中的对象吗?它肯定会进入循环,因为我尝试在里面打印语句。此外,这个 JPanel 对象被放入 TabbedPane 中只是为了澄清。如果我需要更详细地解释或显示更多代码以找到解决方案,请告诉我。谢谢。

JPanel createTipTailoringPanel(TipCalcModel model)
{

    JPanel content = new JPanel();
    int size = model.getNumOfPeople();
    content.removeAll();
    content.updateUI();
    content.setLayout(new GridLayout(0,4));

    JTextField text[] = new JTextField[size];
    JSlider slider[] = new JSlider[size];
    JLabel label[] = new JLabel[size];
    JLabel cash[] = new JLabel[size];


    if(size == 0)
    {
        return content;
    }
    else
    {
        for(int i=0; i<size; i++)
        {
            text[i] = new JTextField();
            slider[i] = new JSlider();
            label[i] = new JLabel("$");
            cash[i] = new JLabel();
            content.add(text[i]);
            content.add(slider[i]);
            content.add(label[i]);
            content.add(cash[i]);
        }

        return content;
    }


}

这是我的调用方法和我用来传递人数的 actionlistener:

 TipCalcView(TipCalcModel model)
{
    setTitle("Tip Calculator");
    JTabbedPane tabbedPane = new JTabbedPane();
    getContentPane().add(tabbedPane);
    tabbedPane.addTab("Main Menu", createMainPanel());
    tabbedPane.setSelectedIndex(0);
    tabbedPane.addTab("Tip Tailoring", createTipTailoringPanel(model));
    tabbedPane.addTab("Config Panel", createConfigPanel());

}

class GuestsListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        int userInput = 0;
        try{
            userInput = m_view.getGuests();
            m_model.setNumOfPeople(userInput);
            m_view.createTipTailoringPanel(m_model);
        }
        catch(NumberFormatException nfex)
        {
            m_view.showError("Bad input: '" + userInput + "'");
        }
    }
}

【问题讨论】:

  • 能贴出调用这个方法的代码吗?

标签: java user-interface dynamic jpanel


【解决方案1】:

我怀疑您的问题不在您列出的代码范围内。这是一个简化的工作示例:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    DynamicJPanel dynamic = new DynamicJPanel();
    frame.add(dynamic.createTipTailoringPanel(3));
    frame.pack();
    frame.setVisible(true);
}

JPanel createTipTailoringPanel(int size) {
    JPanel content = new JPanel();
    content.setLayout(new GridLayout(0, 4));

    for (int i = 0; i < size; i++) {
        content.add(new JTextField());
        content.add(new JSlider());
        content.add(new JLabel("$"));
        content.add(new JLabel());
    }

    return content;
}

【讨论】:

  • 是的,这与我的 JFrame 没有正确实现有关。
【解决方案2】:

首先,由于您不会在任何地方使用数组,因此可以将其缩短为:

JPanel createTipTailoringPanel(TipCalcModel model)
{

    JPanel content = new JPanel();
    int size = model.getNumOfPeople();
    content.setLayout(new GridLayout(0,4));

    if(size == 0)
    {
        return content;
    }
    else
    {
        for(int i=0; i<size; i++)
        {
            content.add(new JTextField());
            content.add(new JSlider());
            content.add(new JLabel("$"));
            content.add(new JLabel());
        }
        return content;
    }
}

第二,好像你在面板中添加了一个空组件,也许这就是你实际得到的?

第三,添加你需要将content面板从上面的方法返回后添加到JFrame(或其他容器)。

【讨论】:

  • 第四和第五,对removeAll()updateUI()的调用是多余的。
  • @afk - 你是对的,我没有看它就复制了,因为数组困扰了我。感谢您指出这一点。
  • 哦,对了,我真的不需要数组,但是当我在更改代码后尝试程序时,它仍然没有显示任何内容我将编辑我的主要帖子并在我调用的地方显示我的代码这种方法。我还注释掉了 removeall 和 updateUI。
  • @Novazero - 这似乎是这个方法之外的问题,正如@WhiteFang34所说,我们需要看看调用这个的方法......
猜你喜欢
  • 2013-09-13
  • 2013-12-07
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 2016-02-01
相关资源
最近更新 更多