Попытка добавить ScrollPane в Jpanel с нулевым макетом внутри BorderLayout - PullRequest
0 голосов
/ 27 августа 2018

Я пытаюсь добавить полосу прокрутки в jpanel с нулевым макетом.

Я хочу создать форму. Это должно всегда отображать несколько кнопок внизу. Любое содержимое внутри формы должно сохранять свой размер и соотношение, даже если размер родительского контейнера был изменен.

Вот то, с чем я пришел. У меня есть панель с borderlayout и добавлены кнопки на юге границы. Затем создал еще одну jpanel, которая будет содержать форму, добавленную в центр родительской jpanel. Поскольку я хочу, чтобы форма сохраняла пропорции, я выбрал нулевую разметку для внутренней панели. Но я хочу, чтобы он отображал полосу прокрутки, когда содержимое не полностью видно. введите описание изображения здесь

Теперь добавление внутренней jpanel в область прокрутки и добавление 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);
}
...