为什么我的滚动窗格没有滚动到任何地方?

gstyhher  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(421)

我不明白为什么这段代码不能显示滚动条。我是一个完全的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);
}
}

我想从那以后我已经对它进行了很大的改进,这是目前的版本:

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

2j4z5cfb1#

不要使用空布局。
不要使用setbounds()。
不要使用setpreferredsize()。
每个回转组件负责确定自己的首选尺寸。布局管理器将设置添加到面板的每个组件的大小/位置,然后(动态)计算面板的首选大小。当面板的首选大小大于滚动窗格的大小时,将显示滚动条。

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.

创建面板时,布局管理器应设置在循环外部。不应为空。
不要硬编码首选尺寸。它不会随着组件的添加而动态调整。
swing组件在默认情况下处于启用状态,因此不需要设置enabled。
不要使用“leftpanel”。只需将滚动窗格直接添加到框架中。这将允许滚动窗格在调整帧大小时动态调整大小。当需要时,滚动条将出现。
不要设置按钮的首选里德大小。大小将根据按钮的文本和字体确定。

相关问题