我的JScrollPane不会在我的JPanel上垂直滚动

时间:2017-03-29 23:58:49

标签: java swing jpanel scrollbar jscrollpane

我有一个代码,我有很多'嵌套'布局。 Border,Flow,在我的主程序中,我有一个GridBag。我正在动态地将面板添加到一个面板,并将其大小设置为使每个元素垂直添加但不会向上或向下滚动。我在网上看了一下,发现了类似的东西,但只有我的水平滚动而不是我想要的垂直滚动。这是我写的测试代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main {

    public Main() {
        JFrame frame = new JFrame("Blah");

        Font font = new Font("Sans-Serif", Font.BOLD, 30);

        JPanel memoryPanel = new JPanel();
        memoryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        //memoryPanel.setPreferredSize(new Dimension(80 * 4, 55 * 9));

        JScrollPane s = new JScrollPane(memoryPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));

        for (int i = 0; i <= 10; i++) {

            JPanel nextPanel = new JPanel();
            nextPanel.setLayout(new BorderLayout());
            nextPanel.setBackground(Color.CYAN);
            nextPanel.setForeground(Color.BLACK);

            JPanel labelPanel = new JPanel();
            labelPanel.setLayout(new BorderLayout());

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));

            JLabel label = new JLabel();
            label.setText("" + i);
            label.setPreferredSize(new Dimension(80 * 4, 55));
            label.setHorizontalAlignment(JLabel.RIGHT);
            label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

            JButton button1 = new JButton("A");
            JButton button2 = new JButton("B");
            JButton button3 = new JButton("C");

            label.setFont(font);
            labelPanel.add(label);

            buttonPanel.add(button1);
            buttonPanel.add(button2);
            buttonPanel.add(button3);

            nextPanel.add(labelPanel, BorderLayout.PAGE_START);
            nextPanel.add(buttonPanel, BorderLayout.PAGE_END);

            memoryPanel.add(nextPanel, 0);
            memoryPanel.revalidate();
            memoryPanel.repaint();
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(s);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}

使用该注释行,它显示水平滚动条。如果没有评论,它没有显示任何。我不确定我是否使用了错误的布局,但我一直试图修复它几个小时。我添加的行是s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));这使得一切都水平而不是垂直。不知道如何解决这个问题。建议?

1 个答案:

答案 0 :(得分:2)

首先摆脱s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));。您不想更改视口的大小,它是您内容的容器,并充当显示组件可见部分的“窗口”。

ScrollPane

接下来,摆脱frame.setResizable(false);,这将允许pack实际打包内容的窗口。因为您使用的是FlowLayout,所以这会使窗口与屏幕一样宽。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Main {

    public Main() {
        JFrame frame = new JFrame("Blah");

        Font font = new Font("Sans-Serif", Font.BOLD, 30);

        JPanel memoryPanel = new JPanel();
        memoryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        //memoryPanel.setPreferredSize(new Dimension(80 * 4, 55 * 9));

        JScrollPane s = new JScrollPane(memoryPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        //        s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));

        for (int i = 0; i <= 10; i++) {

            JPanel nextPanel = new JPanel();
            nextPanel.setLayout(new BorderLayout());
            nextPanel.setBackground(Color.CYAN);
            nextPanel.setForeground(Color.BLACK);

            JPanel labelPanel = new JPanel();
            labelPanel.setLayout(new BorderLayout());

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));

            JLabel label = new JLabel();
            label.setText("" + i);
            label.setPreferredSize(new Dimension(80 * 4, 55));
            label.setHorizontalAlignment(JLabel.RIGHT);
            label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

            JButton button1 = new JButton("A");
            JButton button2 = new JButton("B");
            JButton button3 = new JButton("C");

            label.setFont(font);
            labelPanel.add(label);

            buttonPanel.add(button1);
            buttonPanel.add(button2);
            buttonPanel.add(button3);

            nextPanel.add(labelPanel, BorderLayout.PAGE_START);
            nextPanel.add(buttonPanel, BorderLayout.PAGE_END);

            memoryPanel.add(nextPanel, 0);
            memoryPanel.revalidate();
            memoryPanel.repaint();
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(s);
        //        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }
}

如果要影响可滚动视图区域的大小,则需要创建一个实现Scrollable接口的自定义类,并根据需要返回适当的值