为什么此代码不会在底部带有三个按钮的滚动窗格中显示内容

Why wont this code display content inside of a scrollPane with the three button at bottom

我已将 JFrame 设置为在我的视图末尾可见 class 但我不确定为什么我的自定义 JPanel 仍然不可见 visible.I 我正在尝试简化代码并避免巨大的查看 class,同时实现良好的面向对象编程风格。我的主面板中 JFrame 底部的 JButton 是可见的。我尝试将自定义面板添加到框架中,但它们仍然不可见。

我尝试将所有内容都设置为可见,并且只将自定义 JPanel 添加到 JFrame。

public View(Main pMain) 
{
    setMain(pMain);

    panelClientInfo = new JClientPanel();
    panelPaymentInfo = new JPaymentPanel();
    panelJobDescription = new JJobPanel();
    panelAgreement = new JAgreementPanel();
    clearButton = new JButton("Clear");
    exitButton = new JButton("Exit");
    submitButton = new JButton("Submit");
    panelSecondary = new JPanel();
    panelMain = new JPanel();
    scrollPane = new JScrollPane(panelMain);

    panelSecondary.setLayout(new BoxLayout(panelSecondary, 
    BoxLayout.Y_AXIS));
    panelSecondary.add(panelClientInfo);
    panelSecondary.add(panelJobDescription);
    panelSecondary.add(panelPaymentInfo);
    panelSecondary.add(panelAgreement);
    panelMain.add(panelSecondary, BorderLayout.CENTER);
    panelMain.add(clearButton, BorderLayout.SOUTH);
    panelMain.add(submitButton, BorderLayout.SOUTH);
    panelMain.add(exitButton, BorderLayout.SOUTH);
    scrollPane.add(panelMain);
    scrollPane.setVisible(true);


    setTitle("G.C. Septic Services Contract Drafter");
    setSize(1000, 1000);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(scrollPane);

    setVisible(true);


}

/**Here is a custom JPanel that I am trying to use*/
package contractDrafter;
import javax.swing.JPanel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class JAgreementPanel extends JPanel
{
JPanel panelMain;
JLabel submitterLabel;
JTextField submitterText;

public JAgreementPanel() 
{
    panelMain = new JPanel();
    panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
    submitterLabel = new JLabel("Submitter Name: ");
    submitterText = new JTextField("e.g: Calvin M. Cox", 30);

    panelMain.add(Box.createVerticalGlue());
    panelMain.add(submitterLabel);
    panelMain.add(Box.createVerticalGlue());
    panelMain.add(submitterText);
    panelMain.add(Box.createVerticalGlue());
}

}

我希望这个程序显示不同的 JPanel,这样我岳母只需在完成的程序中输入一些值,它就会为她编写文件,以减少她患有关节炎的手的工作量.我希望看到 JPanel 最终以半整洁到整洁的方式出现在框架上,这样她就可以在框架上上下滚动并输入必要的信息。

您的代码包含很多缺失 类 !!当你 post 你的代码至少可以与 super 类 一起工作时,这样我们就可以理解它是怎么回事了。

无论如何

I have tried to just add the custom panels to the frame but they still are not visible.

这确实与您的代码伙伴冲突! 在代码中

panelMain.add(panelSecondary, BorderLayout.CENTER);
panelMain.add(clearButton, BorderLayout.SOUTH);
panelMain.add(submitButton, BorderLayout.SOUTH);
panelMain.add(exitButton, BorderLayout.SOUTH);

你传递的约束属于 BorderLayout 另一方面你没有将布局设置为 BorderLayout 所以默认情况下它是 FlowLayout

再一次,即使是 BorderLayout 添加相同的 "border" 也会覆盖该边框中的最后一个组件!

您还没有上传图片,但我可以想象按钮在中心水平对齐,这是因为 JPanel 的默认 FlowLayout 布局。

I am hoping to see the JPanels end up on the frame in a semi-neat to neat way so that she can scroll up and down on the frame and enter the necessary information.

好吧,您正在使滚动窗格包含面板和按钮,这是完全错误的(至少在您的设计案例中)。

你应该做的是

JFrame f = new JFrame();
JPanel slidingPanel = new JPanel ();
slidingPanel.setLayout(new BoxLayout(slidingPanel,BoxLayout.Y_AXSIS));
JScrollPane scrollPane = new JScrollPanel (slidingPanel);
f.getContentPane().add(scrollpane,BorderLayout.CENTER);
//then add all of your panels in the slidingpanel
JPanel buttonPanel = new JPanel();
//i can't give you a hint on this , it's almost just designer choice for how you want your buttons to layout
//but add them to the south !!
f.getContentPane().add(buttonPanel,BorderLayout.SOUTH);

如果您的家人的项目还需要一些额外的帮助,我很乐意帮助您,但堆栈溢出不适合您,请将您的项目上传到私人 github 存储库,或者如果您已经有人邀请我了,我的帐户与我在这里的帐户的详细信息相同,伙计;)。

ImageofDesiredGui

所以我终于想通了was.I要感谢@OverLoadedBurden 的快速回复和帮助。我将只提供一个自定义 JPanel class,因为其余的都非常相似,因此没有必要。每当我创建自定义 JPanel 时,面板都不会显示,因为我正在将内容添加到自定义 JPanel 中包含的面板。例如,在我写的旧代码中:

public JAgreementPanel() 
{
    panelMain = new JPanel();
    panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
    submitterLabel = new JLabel("Submitter Name: ");
    submitterText = new JTextField("e.g: Calvin M. Cox", 30);

    panelMain.add(Box.createVerticalGlue());
    //This is where the error exists
    panelMain.add(submitterLabel);
    panelMain.add(Box.createVerticalGlue());
    //This is where the error exists
    panelMain.add(submitterText);
    panelMain.add(Box.createVerticalGlue());
  }
Whereas I should have been adding the desired content of the panel to the custom 
panel 
itself. This is the new correctly functioning code: 
public JAgreementPanel() 
    {
        panelMain = new JPanel();
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        submitterLabel = new JLabel("Submitter Name: ");
        submitterText = new JTextField("e.g: Calvin M. Cox", 30);
        //this could be also written as "this.add(submitterLabel)"
        add(submitterLabel);
        add(Box.createHorizontalGlue());
        add(submitterText);
        add(Box.createHorizontalGlue());
        setVisible(true);
    }
This could also be accomplished with the code comments as well. I will also include 
the updated view constructor that is called to create the gui:
public View(Main pMain) 
{

    setMain(pMain);

    clearButton = new JButton("Clear");
    exitButton = new JButton("Exit");
    submitButton = new JButton("Submit");
    panelSecondary = new JPanel();
    panelMain = new JPanel();


    panelSecondary.setLayout(new BoxLayout(panelSecondary, BoxLayout.Y_AXIS));

    panelSecondary.add(new JAgreementPanel());

    panelSecondary.add(new JClientPanel());

    panelSecondary.add(new JJobPanel());

    panelSecondary.add(new JPaymentPanel());

    panelMain.setLayout(new GridLayout(2,1));

    panelMain.add(panelSecondary);

    JPanel panelButtons = new JPanel();

    panelButtons.add(exitButton);
    panelButtons.add(clearButton);
    panelButtons.add(submitButton);

    panelMain.add(panelButtons);


    scrollPane = new JScrollPane(panelMain);


    scrollPane.setVisible(true);

    add(panelMain);

    setTitle("G.C. Septic Services Contract Drafter");
    setSize(500, 500);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}