Java 尝试在Jpanel中添加滚动窗格,在BorderLayout中使用空布局

Java 尝试在Jpanel中添加滚动窗格,在BorderLayout中使用空布局,java,swing,layout,null,scrollpane,Java,Swing,Layout,Null,Scrollpane,我试图在jpanel中添加一个空布局的滚动条 我想创建一个表单。这应该始终在底部显示几个按钮。表单中的任何内容都应该保持其大小和比例,即使父容器已调整大小 这是我带来的。我有一个带有边框布局的面板,并在边框的南部添加了按钮。然后创建另一个jpanel以包含在父jpanel中心添加的表单。因为我希望表单保持它的比例,所以我对内面板使用空布局。但我希望它在内容不完全可见时显示滚动条 现在,将内部jpanel添加到scrollpane和将scrollpanel添加到父面板(.add(scrollpan

我试图在jpanel中添加一个空布局的滚动条

我想创建一个表单。这应该始终在底部显示几个按钮。表单中的任何内容都应该保持其大小和比例,即使父容器已调整大小

这是我带来的。我有一个带有边框布局的面板,并在边框的南部添加了按钮。然后创建另一个jpanel以包含在父jpanel中心添加的表单。因为我希望表单保持它的比例,所以我对内面板使用空布局。但我希望它在内容不完全可见时显示滚动条

现在,将内部jpanel添加到scrollpane和将scrollpanel添加到父面板(.add(scrollpane,BorderLayout.CENTER))并没有给出所需的格式。 我可以做些什么来获得想要的格式吗

下面是代码示例:

public static void main(String[] args) {
    JFrame jFrame = new JFrame();
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setSize(new Dimension(1000, 700));
    Container c = jFrame.getContentPane();
    c.setLayout(new BorderLayout());
    bottomPanel(c);
    centerPanel(c); //scrollbar should go in this panel
    jFrame.setVisible(true);
}

private static void centerPanel(Container c) {
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(null);
    JButton button = new JButton("This jObject should not resize when window resizes and also should maintain relative position.");
    button.setBounds(new Rectangle(10, 10, 600, 50));
    JButton button1 = new JButton("Just like it works in this code. Just Add ScrollPane to centerPanel That is in green backround");
    button1.setBounds(new Rectangle(10, 70, 600, 50));
    JButton button2 = new JButton("For clearity");
    button2.setBounds(new Rectangle(10, 130, 600, 50));
    centerPanel.add(button);
    centerPanel.add(button1);
    centerPanel.add(button2);
    centerPanel.setBackground(Color.GREEN);
    c.add(centerPanel, BorderLayout.CENTER);
}

private static void bottomPanel(Container c) {
    JPanel bottomPanel = new JPanel(); //Buttons that goes at the bottom of screen will go in here
    JPanel bottomInnerPanel = new JPanel();
    bottomInnerPanel.setLayout(new BorderLayout());
    bottomPanel.setLayout(new GridLayout());
    bottomInnerPanel.add(new JButton("Add"), BorderLayout.WEST);
    bottomInnerPanel.add(new JButton("Search"), BorderLayout.EAST);
    bottomPanel.add(bottomInnerPanel);
    bottomPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    c.add(bottomPanel, BorderLayout.SOUTH);
}

能否将当前代码添加到问题中?请尝试学习
LayoutManager
s。这个概念是完美的(IMHO),但是内置的
GridBagLayout
很难看。因此,我建议你学习一位第三方经理,例如,我需要确信,在我开始新的学习冒险之前,它将满足我想要做的事情。特别是当我在紧张的时间限制。顺便说一句,它是开源的还是至少免费的,因为我现在无法支付任何费用。问题是,没有人会为你重新发明轮子。布局管理器用于解决调整大小的问题。所以,要么你尝试学习
LayoutManager
s,要么你的问题就可能得不到答案(尤其是没有答案)。是的,
MigLayout
是一个免费软件。
表单中的任何内容都应该保持它的大小和比例,即使父容器的大小调整了
,但我希望它在内容不完全可见时显示滚动条
——这些是相互冲突的要求。如果连续缩放组件,则它们将始终适合面板内部,因此无需滚动窗格。我建议只有面板上的某些组件需要调整大小。您可以查看,因为它允许您指定大小调整约束。