在哪里插入JScrollPane?

时间:2014-03-15 10:52:15

标签: java swing jscrollpane

我使用GridBagLayout制作了一个布局,强制子组件从底部向上堆叠。

我唯一剩下的就是让“容器”组件可滚动。

我尝试在不同的位置插入JScrollPane,但我找不到工作点。

我的2个内部JPanels在底部正确对齐,当我调整窗口大小时,它们正确调整大小。但是当我调整窗口的大小以使文本不适合时,我希望“容器”组件滚动。

请帮助新手:)

更新1:滚动应该是整个GridBagLayout。不适用于每个单独的textpane。

更新2:更改代码以改为使用BorderLayout,因为它对我的滚动问题没有任何影响。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class BorderTest extends JFrame {

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new BorderTest().setVisible(true);
        }
    });
}

public BorderTest() {

    UIManager.put("swing.boldMetal", Boolean.FALSE);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Grid Bag");
    setSize(500, 300);
    setLocationRelativeTo(null);

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());
    container.setBorder(new LineBorder(Color.RED, 2));
    add(container);

    JPanel bottom = new JPanel();
    bottom.setBorder(new LineBorder(Color.YELLOW, 2));
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
    container.add(bottom, BorderLayout.SOUTH);

    JTextPane textPane = new JTextPane();
    textPane.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
    textPane.setBorder(new LineBorder(Color.GREEN, 2));
    bottom.add(textPane);

    JTextPane textPane2 = new JTextPane();
    textPane2.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
    textPane2.setBorder(new LineBorder(Color.BLUE, 2));
    bottom.add(textPane2);

}

}

运行上述代码时的屏幕截图:

enter image description here

当我缩小窗口时,项目不适合(当然)然后我想要显示垂直滚动条(但不是水平的,因为单词应该换行)

enter image description here

正如您所看到的,“红色”边框位于窗口的“外部”,因此在此处放置滚动窗格似乎是一个好主意。以下是添加了滚动窗格的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class BorderTest extends JFrame {

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new BorderTest().setVisible(true);
        }
    });
}

public BorderTest() {

    UIManager.put("swing.boldMetal", Boolean.FALSE);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Grid Bag");
    setSize(500, 300);
    setLocationRelativeTo(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBorder(new LineBorder(Color.CYAN, 2));
    add(scrollPane);

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());
    container.setBorder(new LineBorder(Color.RED, 2));
    scrollPane.setViewportView(container);

    JPanel innerContainer = new JPanel();
    innerContainer.setLayout(new BorderLayout());
    innerContainer.setBorder(new LineBorder(Color.PINK, 2));
    scrollPane.setViewportView(innerContainer);

    JPanel bottom = new JPanel();
    bottom.setBorder(new LineBorder(Color.YELLOW, 2));
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
    innerContainer.add(bottom, BorderLayout.SOUTH);

    JTextPane textPane = new JTextPane();
    textPane.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
    textPane.setBorder(new LineBorder(Color.GREEN, 2));
    bottom.add(textPane);

    JTextPane textPane2 = new JTextPane();
    textPane2.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
    textPane2.setBorder(new LineBorder(Color.BLUE, 2));
    bottom.add(textPane2);

}

}

但结果不是我想要的。现在添加了水平的scoll bar并使我的文本项成为一个巨大的行:

enter image description here

1 个答案:

答案 0 :(得分:5)

要向组件添加JScrollPane,只需使用该组件创建JScrollPane,然后将JScrollPane添加到您想要组件的面板中。

所以而不是:

panel.add(component, constraints);

使用

JScrollPane scrollPane = new JScrollPane(component);
panel.add(scrollPane, constraints);

当然,您可以使用scrollPane变量自定义JScrollPane,查看文档,但是通过简单地将其放入您的代码中我们有:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class GridBag extends JFrame
{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                new GridBag().setVisible(true);
            }
        });
    }

    public GridBag()
    {

        UIManager.put("swing.boldMetal", Boolean.FALSE);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Grid Bag");
        setSize(500, 300);
        setLocationRelativeTo(null);

        JPanel container = new JPanel(new GridBagLayout());
        container.setBorder(new LineBorder(Color.RED, 2));
        add(container);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.LAST_LINE_START;

        JPanel bottom = new JPanel();
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
        bottom.setBorder(new LineBorder(Color.GREEN, 2));
        container.add(bottom, c);

        JTextPane textPane = new JTextPane();
        textPane.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
        textPane.setBorder(new LineBorder(Color.BLACK, 2));
        bottom.add(new JScrollPane(textPane));

        JTextPane textPane2 = new JTextPane();
        textPane2.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
        textPane2.setBorder(new LineBorder(Color.BLUE, 2));
        bottom.add(new JScrollPane(textPane2));
    }
}

并使它看起来像这样:

enter image description here

哪个有效,但看起来不太好,所以让我向您展示如何使用GridBagConstraints.weighty属性进行正确的高度分布。

我要做的是在GridBagLayout(2个单元格)上添加两个面板,然后使用weighty数字告诉他们要占用多少空间。为此,我想将c.fill更改为BOTH

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class GridBag extends JFrame
{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                new GridBag().setVisible(true);
            }
        });
    }

    public GridBag()
    {

        UIManager.put("swing.boldMetal", Boolean.FALSE);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("Grid Bag");
        setSize(500, 300);
        setLocationRelativeTo(null);

        JPanel container = new JPanel(new GridBagLayout());
        container.setBorder(new LineBorder(Color.RED, 2));
        add(container);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH; //make it fill both directions
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 1.0; //modify this number to change the proportion of the screen division
        c.anchor = GridBagConstraints.CENTER; //for this example anything here goes.

        JPanel topPanel = new JPanel();
        topPanel.add(new JTextField("Put other components here"));
        //topPanel.setBorder(new LineBorder(Color.YELLOW, 2));
        container.add(topPanel, c);

        c.gridy = 1;
        c.weighty = 1.0; //modify this number to change the proportion of the screen division

        JPanel bottom = new JPanel();
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
        bottom.setBorder(new LineBorder(Color.GREEN, 2));
        container.add(bottom, c);

        JTextPane textPane = new JTextPane();
        textPane.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
        textPane.setBorder(new LineBorder(Color.BLACK, 2));
        bottom.add(new JScrollPane(textPane));

        JTextPane textPane2 = new JTextPane();
        textPane2.setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et ");
        textPane2.setBorder(new LineBorder(Color.BLUE, 2));
        bottom.add(new JScrollPane(textPane2));
    }
}

看起来像这样:

enter image description here

这需要顶部组件的一半屏幕和两个textPanes的一半,这是因为两个单元格weightytopPanelbottom)都是1.0,相同价值,所以比例是1:1。您可以更改这些数字以更改它们占用的空间。例如,将第一个权重设为0.5将使其成为1:2的比例。