动态 JPanel 对象不显示

发布于 2024-11-06 05:01:12 字数 1895 浏览 0 评论 0原文

有人可以看一下我的代码的这一部分并告诉我为什么它不会返回 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;
    }


}

这是我的调用方法和用于传递 numberofpeople 的动作侦听器:

 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 + "'");
        }
    }
}

Can someone take a look at this part of my code and tell me why it won't return the objects inside the JPanel? It definitely goes inside the loop since I tried printing statements inside. Also this JPanel object is being put inside a TabbedPane just for clarification. Let me know if I need to explain in more detail or show more code to find a solution. Thanks.

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;
    }


}

Here is my calling method and the actionlistener that I use to pass in the numberofpeople:

 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 + "'");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海不蓝 2024-11-13 05:01:12

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

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;
}

I suspect that your problem is outside of the code you listed. Here's a simplified working example:

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;
}
夏日落 2024-11-13 05:01:12

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

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(或其他容器)。

First, since you don't use the arrays anywhere, it could be shortened to:

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;
    }
}

Second, seems like you add an empty components to the panel, maybe that's what you actually get?

Third, add you need to add the content panel to the JFrame (or other container) once it returns from the method above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文