我有一个gui,它的Panel包含一系列标签和TextField,并使用spring布局(这是mainPanel),而另一个Panel仅包含button(buttonPanel)。我正在尝试使我的mainPanel也具有垂直滚动条。我想实现我的GUI,以便在JFrame中有2个面板。 mainPanel出现在框架的顶部,而buttonPanel出现在mainPanel的下方。

我的问题是我无法使面板出现,使得buttonPanel在mainPanel下方,并且我也不确定如何向mainPanel添加滚动条。任何帮助,将不胜感激。

编辑:我能够解决有关JPanels的问题,现在我唯一的问题是我无法让mainPanel滚动。我在下面添加了我的最新代码:

这是我到目前为止的代码:

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);



        }

最佳答案

为了使其可滚动,我只需要增加mainPanel的首选大小,使其大于滚动条即可。

 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);
        }

关于java - 将JPanel添加到JScrollPane,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60588250/

10-11 03:58