【问题标题】:Panel shows nothing if setLayout is null如果 setLayout 为空,面板不显示任何内容
【发布时间】:2026-01-31 08:35:01
【问题描述】:

我创建了一个 JPanel 并向其中添加了一个 JTabbedPane。然后,我在此选项卡窗格中添加了另外两个面板,并将所有面板的布局设置为 null。但是 Frame 没有显示任何内容,如果我将主面板的布局从 null 更改为 BorderLayout 或 GridLayout,那么它可以完美运行。有人可以告诉我这里有什么问题.. 提前谢谢

我的代码: 我正在为每个组件创建对象,并通过检查空约束在指定的 getter 中为它们设置边界

第一面板:

public class InsurancePanel extends JPanel
{
public InsurancePanel()
{
    getJpInsurance();
}

private static final long   serialVersionUID    = 1L;

public void getJpInsurance()
{
    setLayout(null);
    add(getJlLICName());
    add(getJlCompany());

    add(getJtfLICName());
    add(getJtfCompany());

    add(getJbUpdate());
}
}

第二个小组:

public class PatientPanel extends JPanel
{

public PatientPanel()
{
    getJpPatient();
 }

private static final long   serialVersionUID    = 1L;

public void getJpPatient()
{
    setLayout(null);
    add(getJlFirstName());
    add(getJlLastName());

    add(getJtfFirstName());
    add(getJtfLastName());

    add(getJbNew());
}
}

还有主面板:

public class MainPanel extends JPanel
{
private PatientPanel        m_PatientPanel;
private InsurancePanel      m_InsurancePanel;
private JTabbedPane jtpView;

public void designMainPanel()
{
    setLayout(new GridLayout()); // if it is null, then nothing shows in the frame
    setSize(650, 520);
    setBounds(0, 0, 650, 520);
    add(getJtpView());
}

public JTabbedPane getJtpView()
{
    if (jtpView == null)
    {
        jtpView = new JTabbedPane();
        jtpView.addTab("Patient", getPatientPanel());
        jtpView.addTab("Insurance", getInsurancePanel());
    }
    return jtpView;
}

    public PatientPanel getPatientPanel()
{
    if (m_PatientPanel == null)
    {
        m_PatientPanel = new PatientPanel();
    }
    return m_PatientPanel;
}

public InsurancePanel getInsurancePanel()
{
    if (m_InsurancePanel == null)
    {
        m_InsurancePanel = new InsurancePanel();
    }
    return m_InsurancePanel;
}

}

【问题讨论】:

  • 在将组件添加到空布局时,您应该指定子组件的边界,否则您的子组件的维度为 0 并且看不到但使用布局。使用空布局非常糟糕。
  • 是的,但我不熟悉秋千。所以我想让它在没有布局的情况下工作。我也尝试设置边界,即使它不起作用
  • 那么你应该发布你的代码来看看有什么问题..好的。通过添加你的不工作的代码来编辑你的问题
  • 用源代码编辑了我的问题,所以请看一下

标签: java jpanel jtabbedpane


【解决方案1】:

问题出在这个方法上

public void designMainPanel()
{
    setLayout(null); // if it is null, then nothing shows in the frame
    setSize(650, 520);
    setBounds(0, 0, 650, 520); // ←---- problem is here
    add(getJtpView());
}

当您将子组件添加到父容器时,您应该指定子组件的 x、y 位置和宽度、高度。 在此代码中,您的父组件是 MainPanel 并且您正在尝试添加 getJtpView() JTabbedPane 这是子组件。但是您将边界设置为父组件而不是子组件 (getJtpView() JTabbedPane)。

你应该把它改成

public void designMainPanel() {
    setLayout(null); //here it's null but it's working now
    JTabbedPane tab1 = getJtpView();
    tab1.setBounds(0, 0, 650, 520); //←---fixed:set bound to child JTabbedPane 
    add(tab1);
}

如您所见,我们为子组件-jtabbedpane 设置了绑定

tab1.setBounds(0, 0, 650, 520);

【讨论】:

  • 感谢您的解决方案。它工作得很好,但是当我第一次启动我的应用程序时它显示 PatientPanel 但两个选项卡不可见。当我单击选项卡所在的位置时,它会更改为其他面板。并且标签是可见的。
  • @SaikiranGosikonda 如果你可以把你的所有课程都发到这里而不是在pastebin.com 这样的地方,我可以检查它并提供帮助。
  • 好的,我找到了问题所在。我在一个框架上调用 setVisible(true) 两次。并感谢您的回答
最近更新 更多