为什么哦,为什么我的 scrollPane 没有滚动到任何地方?

Why oh why, does my scrollPane not scroll to anywhere?

我不明白为什么这段代码不显示滚动条。我是 Swing 的完全初学者,所以滚动窗格非常混乱,我不理解我在网上看到的一些解决方案。烦人的是这段代码短暂地工作但是我在尝试添加组件时在备份之前销毁了成功的部分。如有任何帮助,我们将不胜感激!

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

public class Test extends JFrame
{
    private JPanel leftPanel;
    private JButton myButton;
    private JPanel scrollPanel;
    private JScrollPane scrollPane;
    
    public static void main(String[] args)
    {
        System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run() {
                new Test();
            }
        });
    }
    
    /**
     * Constructor for objects of class Backup
     */
    public Test()
    {
        this.setTitle("testy");
        this.setSize(new Dimension(1280,622));
        
        leftPanel = new JPanel();
        leftPanel.setBackground(new Color(255,0,0));
        leftPanel.setBounds(0, 100, 640, 558);
        leftPanel.setEnabled(true);
        this.add(leftPanel);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(new Color(100,100,100));
        scrollPanel.setPreferredSize(new Dimension(640, 550));
        scrollPanel.setEnabled(true);
        
        JScrollPane scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(0, 0, 642, 550);
        scrollPane.setEnabled(true);
        leftPanel.add(scrollPane);
        
        for (int i = 1; i < 30; i++) 
        {
            scrollPanel.setLayout(new GridLayout(30, 1, 0, 1));
            myButton = new JButton("AAAAAAAAAAAAAAAAAAAAA " + i);
            myButton.setPreferredSize(new Dimension(630, 70));
            scrollPanel.add(myButton);
        }
        
        this.setBackground(new Color(0,0,0));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
}
}

感谢 camickr,我想从那时起我已经大大改进了它 这是当前版本:

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

    public class Test extends JFrame
    {
    private JPanel leftPanel;
    private JButton myButton;
    private JPanel scrollPanel;
    private JScrollPane scrollPane;

    public static void main(String[] args)
    {
    System.setProperty("swing.defaultlaf", 
    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
        public void run() {
            new Test();
        }
    });
    }


    public Test()
    {
    this.setTitle("testy");
    this.setSize(new Dimension(1280,622));
    
    leftPanel = new JPanel();
    leftPanel.setBackground(new Color(255,0,0));
    leftPanel.setBounds(0, 100, 640, 558);
    this.add(leftPanel);
    
    scrollPanel = new JPanel();
    scrollPanel.setLayout(new GridLayout(70, 1, 0, 1));
    //scrollPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 
    20));
    scrollPanel.setBackground(new Color(100,100,100));
    //scrollPanel.setPreferredSize(new Dimension(640, 550));
    
    //JScrollPane scrollPane = new JScrollPane(scrollPanel);
    JScrollPane scrollPane = new JScrollPane(scrollPanel, 
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    //scrollPane.setLayout(new GridLayout(30, 1, 0, 1));
    //scrollPane.setBounds(0, 0, 642, 550);
    scrollPane.setEnabled(true);
    //scrollPanel.add(scrollPane);
    
    leftPanel.add(scrollPanel);

    for (int i = 1; i < 71; i++) 
    {
        myButton = new JButton("AAAAAAAAAAAAAAAAAAAAA " + i);
        //myButton.setPreferredSize(new Dimension(640, 80));
        scrollPanel.add(myButton);
    }
    
    this.setBackground(new Color(0,0,0));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    this.setVisible(true);
    }}
  1. 不要使用空布局。

  2. 不要使用 setBounds()。

  3. 不要使用 setPreferredSize()。

每个 Swing 组件负责确定自己的首选大小。然后布局管理器将设置添加到面板的每个组件的 size/location,然后它将(动态地)计算面板的首选大小。当面板的首选尺寸大于滚动窗格的尺寸时,将出现滚动条。

    scrollPanel = new JPanel(null);
    scrollPanel.setBackground(new Color(100,100,100));
    // scrollPanel.setPreferredSize(new Dimension(640, 550)); // delete
    //scrollPanel.setEnabled(true); // not needed
    ...

    for (int i = 1; i < 30; i++) 
    {
        //scrollPanel.setLayout(new GridLayout(30, 1, 0, 1)); // set layout when panel created.
        //myButton.setPreferredSize(new Dimension(630, 70)); // not needed.
  1. 布局管理器应该在创建面板时设置在循环之外。它不应该为空。

  2. 不要硬编码首选尺寸。它不会随着组件的添加而动态调整。

  3. Swing组件默认启用,不需要setEnabled。

  4. 不要使用“leftPanel”。只需将滚动窗格直接添加到框架中即可。这将允许滚动窗格在调整框架大小时动态调整大小。然后在需要的时候会出现滚动条。

  5. 不要设置按钮的首选尺寸。大小将根据按钮的文本和字体确定。