我将JPanels添加到JFrame时遇到了一些麻烦 . 我的JFrame有一个GridBagLayout,我以水平方式添加这些JPanels,我测试了两个案例,第一个显示55个JPanels连续排列有滚动条,第二个没有工作,它必须显示76 JPanels但结果非常奇怪:

我不发布代码因为我认为它与代码无关,也许是使用Swing进行GUI编程的限制 . 我找不到任何有关此(限制)或发生在我身上的相同问题的信息 .

谢谢

编辑

好的,我使用了以下代码:

import javax.swing.*;
import java.awt.*;

public class TestingFrame extends JPanel {

    public TestingFrame() {

        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;

        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.fill = GridBagConstraints.BOTH;


        int numOfPanels = 10000;

        for (int i = 0; i < numOfPanels; ++i) {
            JPanel toAdd = new JPanel();
            JButton tmp = new JButton("HELLO IT'S ME");

            toAdd.add(tmp);

            gbc.gridx = i;
            this.add(toAdd, gbc);
        }

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        TestingFrame view = new TestingFrame();

        JScrollPane scrPane = new JScrollPane(view);

        frame.add(scrPane);
        frame.pack();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

此代码重现了之前告诉您的错误 . 请注意,由于我现在使用的JPanel的组件数量,需要更多的面板来重现错误,它们比原来的小(只有一个按钮) .