为什么此代码不将滚动按钮底部的三个按钮显示在内容中?

时间:2019-05-01 18:35:56

标签: java user-interface jframe jpanel

我已经将JFrame设置为在视图类末尾可见,并且不确定为什么自定义的JPanels仍然不可见,我正在尝试简化代码并避免使用大型View类,同时都实现了良好的对象面向编程风格。主面板中JFrame底部的JButton是可见的。我试图将自定义面板添加到框架,但它们仍然不可见。

我尝试将所有内容设置为可见,并且仅将自定义JPanels添加到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());
}

}

我希望该程序显示不同的JPanels,以便我婆婆要做的就是在已完成的程序中键入一些值,然后它将为她编写文件,以减轻关节炎患者的工作量。我希望看到JPanels以整洁的方式最终出现在框架上,以便她可以在框架上上下滚动并输入必要的信息。

2 个答案:

答案 0 :(得分:0)

您的代码包含很多缺少的类!当您发布代码时,至少要使用超类,以便我们了解如何处理它。

任何方式

  

我试图将自定义面板添加到框架中,但是它们仍然不可见。

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

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

您通过的约束属于BorderLayout,而您尚未将布局设置为BorderLayout,因此默认情况下为FlowLayout

即使是在BorderLayout中添加相同的“边框”,也将覆盖该边框中的最后一个分量!

您尚未上传图片,但是我可以想象按钮在中心水平对齐,这是由于FlowLayout的默认JPanel布局。

  

我希望看到JPanels以整洁的方式最终出现在框架上,以便她可以在框架上上下滚动并输入必要的信息。

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

你应该做的就像

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存储库中,或者如果您已经有一个邀请我,我的帐户的详细信息与我在这里的帐户相同;)。

答案 1 :(得分:0)

ImageofDesiredGui

所以我终于弄清楚了它是什么,我要感谢@OverLoadedBurden的快速回复和帮助。我将只提供一个自定义的JPanel类,因为其余的是如此相似,以至于不必要。每当创建自定义JPanel时,面板都不会显示,因为我将内容添加到了自定义JPanel中包含的面板中。例如,在旧代码中,我写了:

type DiscriminatedUnion<V extends Union["tag"]> = Extract<Union, Record<"tag", V>>;
let shouldBeTypeA: DiscriminatedUnion<"a">; // TypeA, hooray!