尝试在Borderpanel内以空布局在Jpanel中添加ScrollPane

时间:2018-08-27 10:22:02

标签: java swing layout null scrollpane

我正在尝试在jpanel中添加具有空布局的滚动条。

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

这就是我随附的内容。我有一个带有borderlayout的面板,并在border的南部添加了按钮。然后创建另一个jpanel以包含添加到父jpanel中心的表单。因为我想保持表格的比例,所以我将内部面板的布局设为空。但是我希望它在内容不完全可见时显示滚动条。 enter image description here

现在将内部jpanel添加到滚动窗格并将滚动面板添加到父面板(.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);
}

0 个答案:

没有答案