滚动窗格带有嵌套布局的问题

时间:2015-06-16 01:30:03

标签: java

我无法将滚动窗格添加到嵌套面板。这就是我所拥有的:

public class board {
    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new GridLayout(1, 0));

        JPanel left = new JPanel();
        pane.add(left);
        left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));

        JPanel leftTop = new JPanel();
        leftTop.setPreferredSize(new Dimension(266, 300));
        leftTop.setBackground(Color.black);
        left.add(leftTop);

        JScrollPane scrollPane = new JScrollPane(leftTop); //problem is here
        left.add(scrollPane); 

        JButton jb = new JButton();
        jb.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb);

        JButton jb1 = new JButton();
        jb1.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb1);

        JButton jb2 = new JButton();
        jb2.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb2);

        JButton jb3 = new JButton();
        jb3.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb3);

        JButton jb4 = new JButton();
        jb4.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb4);

        JButton jb5 = new JButton();
        jb5.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb5);

        JButton jb6 = new JButton();
        jb6.setPreferredSize(new Dimension(250,50));
        leftTop.add(jb6);

        JPanel leftBottom = new JPanel();
        leftBottom.setPreferredSize(new Dimension(266, 300));
        leftBottom.setBackground(Color.red);
        left.add(leftBottom);

        JPanel middle = new JPanel();
        pane.add(middle);
        middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));

        JPanel middleTop = new JPanel();
        middleTop.setPreferredSize(new Dimension(266, 200));
        middleTop.setBackground(Color.green);
        middle.add(middleTop);

        JPanel middleBottom = new JPanel();
        middleBottom.setPreferredSize(new Dimension(266, 400));
        middleBottom.setBackground(Color.yellow);
        middle.add(middleBottom);

        JPanel right = new JPanel();
        right.setPreferredSize(new Dimension(266, 600));
        right.setBackground(Color.blue);

        pane.add(right);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("GridBagLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

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

我只是搞乱了JPanels,我似乎无法向左上方窗格添加滚动。我想我正确地初始化了scrollPane,但是我是否将它添加到了错误的窗格中?

1 个答案:

答案 0 :(得分:0)

你最初的问题在这里

leftTop.setPreferredSize(new Dimension(266, 300));

这将覆盖布局管理器(在这种情况下为FlowLayout)否则将向JScrollPane提供的内容,以便它知道如何管理视图(例如何时显示滚动条)

您将遇到的下一个问题是,FlowLayout无法达到您想要的效果。相反,您可能希望使用GridLayoutGridBagLayout代替

ExampleLayout

JPanel leftTop = new JPanel(new GridBagLayout());
//leftTop.setPreferredSize(new Dimension(266, 300));
leftTop.setBackground(Color.black);

JScrollPane scrollPane = new JScrollPane(leftTop); //problem is here
left.add(scrollPane);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(5, 10, 5, 10);
JButton jb = new JButton();
jb.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb, gbc);

JButton jb1 = new JButton();
jb1.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb1, gbc);

JButton jb2 = new JButton();
jb2.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb2, gbc);

JButton jb3 = new JButton();
jb3.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb3, gbc);

JButton jb4 = new JButton();
jb4.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb4, gbc);

JButton jb5 = new JButton();
jb5.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb5, gbc);

JButton jb6 = new JButton();
jb6.setPreferredSize(new Dimension(250, 50));
leftTop.add(jb6, gbc);

知道,如果这不符合您的需求,您将需要创建一个实现Scrollable的自定义组件,这将允许您指定PreferredScrollableViewportSize,它将告诉JScrollPane应该是可视区域的首选大小,而不是使用视图的preferredSize