Adding JPanel to JScrollPane

2020-04-17 08:00发布

问题:

I have a gui which has a Panel that contains a sequence of labels and TextFields and uses a spring layout(this is the mainPanel) and another Panel that just contains a button(buttonPanel). I am trying to make my mainPanel to have a vertical scrollbar as well. I would like to implement my GUI such that within the JFrame I have 2 panels. The mainPanel appears on the top of the frame and the buttonPanel appears below the mainPanel.

My problem is I am not able to make the Panels appear such that the buttonPanel is below the mainPanel and I am also not sure how to add a scrollbar to the mainPanel. Any help would be appreciated.

EDIT : I was able to solve my issue regarding the JPanels, now my only problem is that I cant get my mainPanel to scroll. I've added my most recent code below :

Here is the code I have so far:

public static void main(String args[]) {


            JFrame frame = new JFrame("SpringLayout");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JScrollPane scroll = new JScrollPane();
            Container contentPane = frame.getContentPane();

           JButton next = new JButton("Next");
           JPanel buttonPanel = new JPanel();
           buttonPanel.add(next);
            SpringLayout layout = new SpringLayout();
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(layout);
            contentPane.setLayout(new BorderLayout());


            int j = 25;
            for(int i =0;i<150;i++){
                JLabel label = new JLabel("Enter Name " + i );
                JTextField text = new JTextField(15);

            mainPanel.add(label);
            mainPanel.add(text);
            layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
                            contentPane);
            layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH,
                            contentPane);
            layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH,
                            contentPane);
            layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST,
                            label);
            j+=30;
            }
            //mainPanel.setSize(500,800);
            scroll.setPreferredSize(new Dimension(500,500));
            scroll.setViewportView(mainPanel);
            contentPane.add(scroll);
            contentPane.add(buttonPanel,BorderLayout.SOUTH);
            //mainWindow.add(contentPane);
            frame.setSize(500, 600);
            frame.setVisible(true);



        }

回答1:

To make it scrollable I just needed to increase the preferred size of my mainPanel such that it would be bigger than the scrollbar.

 public static void main(String args[]) {
                JFrame frame = new JFrame("SpringLayout");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JScrollPane scroll = new JScrollPane();
                Container contentPane = frame.getContentPane();

               JButton next = new JButton("Next");
               JPanel buttonPanel = new JPanel();
               buttonPanel.add(next);
                SpringLayout layout = new SpringLayout();
                JPanel mainPanel = new JPanel();
                mainPanel.setLayout(layout);
                contentPane.setLayout(new BorderLayout());


                int j = 25;
                for(int i =0;i<18;i++){
                    JLabel label = new JLabel("Enter Name " + i );
                    JTextField text = new JTextField(15);

                mainPanel.add(label);
                mainPanel.add(text);
                layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
                                contentPane);
                layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH,
                                contentPane);
                layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH,
                                contentPane);
                layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST,
                                label);
                j+=30;
                }
                mainPanel.setPreferredSize(new Dimension(mainPanel.getWidth(), 1500));
                scroll.setPreferredSize(new Dimension(500,500));
                scroll.setViewportView(mainPanel);
                contentPane.add(scroll);
                contentPane.add(buttonPanel,BorderLayout.SOUTH);
                //mainWindow.add(contentPane);
                frame.setSize(500, 600);
                frame.setVisible(true);
        }


回答2:

  • I can't comment something, to try and compare

  • notice in this moment I don't understand why SpringLayout and JFrame#pack() doesn't build proper GUI based on PreferredSize, bump in this looks like as (in this moment) my issue too

code with hardcoded JFrame.setSize() instead of proper JFrame#pack()

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

public class Main {

    public Main() {
        JFrame frame = new JFrame("SpringLayout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton next = new JButton("Next");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(next);
        SpringLayout layout = new SpringLayout();
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(layout);
        int j = 25;
        for (int i = 0; i < 5; i++) {
            JLabel label = new JLabel("Enter Name");
            JTextField text = new JTextField(15);
            layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
                    mainPanel);
            layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH,
                    mainPanel);
            layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH,
                    mainPanel);
            layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST,
                    label);
            j += 30;
            mainPanel.add(label);
            mainPanel.add(text);
        }
        frame.add(mainPanel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                 Main mn = new Main();
            }
        });
    }
}


回答3:

This is how I would do it:

public static void main(String args[]) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JButton next = new JButton("Next");
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(next);
    GridBagLayout layout = new GridBagLayout();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(layout);
    contentPane.setLayout(new BorderLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    int j = 25;
    for (int i = 0; i < 50; i++) {
        JLabel label = new JLabel("Enter Name (" + i + ")");
        JTextField text = new JTextField(15);

        gbc.gridx = 0;
        gbc.gridy = i;

        mainPanel.add(label, gbc);

        gbc.gridx = 1;
        mainPanel.add(text, gbc);
    }
    contentPane.add(new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
    frame.setSize(500, 800);
    frame.setVisible(true);
}

Few modifications: * use GridBagLayout instead of SpringLayout (just because I don't know SpringLayout) * wrap your mainPanel inside a JScrollPane

Does not look and feel exactly like your example. GridBagConstraints can be tuned.