1

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

我的问题是我无法使面板显示为使 buttonPanel 位于 mainPanel 下方,而且我也不确定如何向 mainPanel 添加滚动条。任何帮助,将不胜感激。

编辑:我能够解决我关于 JPanel 的问题,现在我唯一的问题是我无法让我的 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);



        }
4

3 回答 3

3

为了使其可滚动,我只需要增加我的 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);
        }
于 2012-10-23T15:08:44.183 回答
1
  • 我无法发表评论,尝试比较

  • 请注意,此时我不明白为什么SpringLayout并且JFrame#pack()没有基于 构建适当的 GUI PreferredSize,这看起来像(此时)我的问题

硬编码JFrame.setSize()而不是正确的代码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();
            }
        });
    }
}
于 2012-10-23T14:02:45.917 回答
0

我会这样做:

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

一些修改: * 使用 GridBagLayout 而不是 SpringLayout(只是因为我不知道 SpringLayout) * 将你的 mainPanel 包装在 JScrollPane 中

看起来和感觉与您的示例完全不同。GridBagConstraints 可以调整。

于 2012-10-23T14:01:56.450 回答