如何在BorderLayout的CENTER中使用JScrollpane

时间:2012-02-07 16:55:57

标签: java swing jscrollpane

我有以下代码:

    package example;

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;

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

    public class ScrollingExample extends JFrame
    {
        private static final long serialVersionUID = 1L;

        public static void main(String[] args)
        {
            ScrollingExample scrollingExample = new ScrollingExample();
            scrollingExample.go();
        }

        private void go()
        {
            JPanel topPanel = new JPanel();
            topPanel.add(new JLabel("boo"));

            JPanel mainPanel = new JPanel();
            JPanel mainInnerPanel = makeMainInnerPanel();

            JScrollPane scrollPane = new JScrollPane(mainInnerPanel);
            mainPanel.add(scrollPane);
            add(topPanel, BorderLayout.NORTH);
            add(mainPanel, BorderLayout.CENTER);

            pack();
            setVisible(true);
        }

        private JPanel makeMainInnerPanel()
        {
            JPanel row1 = makeHorizontalPanel("one", "two", "three", "four", "five");
            JPanel row2 = makeHorizontalPanel("six", "seven", "eight");
            JPanel row3 = makeHorizontalPanel("nine", "ten", "eleven");
            JPanel mainInnerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            mainInnerPanel.add(row1);
            mainInnerPanel.add(row2);
            mainInnerPanel.add(row3);
            return mainInnerPanel;
        }

        private JPanel makeHorizontalPanel(String ... labelValues)
        {
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            for (String s : labelValues)
            {
                JLabel label = new JLabel(s);
                panel.add(label);
            }
            return panel;
        }
    }

当我显示它时,它会自动调整尺寸以显示所有标签;当我调整窗口大小时,我无法显示滚动条。

当我开始工作时,我想创建一个包含JTabbedPane和几个按钮的面板,如果用户调整窗口大小以便无法看到某些部分,我希望它具有滚动条。如果这个问题的答案需要额外的或不同的东西,那么提及它将是值得赞赏的。

3 个答案:

答案 0 :(得分:5)

当窗口大小发生变化时,您需要允许滚动窗格增大/缩小。

不需要“mainPanel”。

只需使用:

// add(mainPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.CENTER);

答案 1 :(得分:3)

JPanel默认情况下FlowLayout,你必须准确定义(如果你想改变LayoutManager),JComponent已经实现了各种LayoutManagers,例如JFrame / JDialog /JWindow(来自Java5)默认情况下为Borderlayout

mainPanel.setLayout(new Borderlayout(10, 10))

答案 2 :(得分:3)

import java.awt.BorderLayout;
import java.awt.FlowLayout;

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

public class ScrollingExample extends JFrame
{
    private static final long serialVersionUID = 1L;

    public static void main(String[] args)
    {
        ScrollingExample scrollingExample = new ScrollingExample();
        scrollingExample.go();
    }

    private void go()
    {
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("boo"));

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel mainInnerPanel = makeMainInnerPanel();

        JScrollPane scrollPane = new JScrollPane(mainInnerPanel);
        mainPanel.add(scrollPane);
        add(topPanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);

        pack();
        setVisible(true);
    }

    private JPanel makeMainInnerPanel()
    {
        JPanel row1 = makeHorizontalPanel("one", "two", "three", "four", "five");
        JPanel row2 = makeHorizontalPanel("six", "seven", "eight");
        JPanel row3 = makeHorizontalPanel("nine", "ten", "eleven");
        JPanel mainInnerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        mainInnerPanel.add(row1);
        mainInnerPanel.add(row2);
        mainInnerPanel.add(row3);
        return mainInnerPanel;
    }

    private JPanel makeHorizontalPanel(String ... labelValues)
    {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        for (String s : labelValues)
        {
            JLabel label = new JLabel(s);
            panel.add(label);
        }
        return panel;
    }
}