9

我想实现以下目标:

+------------------Other container(s)-----------------+
|        +------JScrollPane (vertical)-------+        |
|        | JTextField                        |        |
|        | Box.createRigidArea (vertical)    |        | 
|        | JTextArea                         |        |
|        | { etc.. any other J-component }   |        |
|        |                                   |        |
|        |                                   |        |
|        |                                   |        |
|        |                                   |        |
|        +-----------------------------------+        |
+-----------------------------------------------------+

我能得到的最接近的是以下(伪)代码:

JPanel container = new JPanel(new BorderLayout());
JPanel innerContainer = new JPanel();
innerContainer.setLayout(new BoxLayout(_innerContainer, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(innerContainer);
container.add(scrollPane, BorderLayout.NORTH);

每当我想添加组件时,它们都会被添加到内部容器中:

innerContainer.add(new JTextField());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(new JTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(new JLabel());

.... ETC

我面临以下问题:

我让布局管理器处理所有 UI 元素的大小。所以最好我不使用任何 .setxxSize() 方法。一个被大量文本占据的 JTextArea 将使用所有需要的空间(垂直和水平)。这完全没问题。每当调整框架大小时,JTextArea 都会增长,这仍然很好。但是每当我缩小框架时,JTextArea 不会相应地调整。因此会出现一个水平滚动条,但我想避免这种情况。我希望用户只垂直滚动。

有没有人有一些好的提示?谢谢!

编辑:

SSCCE:

import java.awt.*;

import javax.swing.*;

public class VerticalStackPanel extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new VerticalStackPanel();
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public VerticalStackPanel() {
        super();

        JScrollPane scrollPane = new JScrollPane(createVerticalStackPanel());
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        getContentPane().add(scrollPane);
    }

    private Component createVerticalStackPanel() {
        JPanel container = new JPanel(new BorderLayout());
        container.add(createInnerContainer(), BorderLayout.NORTH);

        return container;
    }

    private Component createInnerContainer() {
        JPanel innerContainer = new JPanel();
        innerContainer.setLayout(new BoxLayout(innerContainer, BoxLayout.Y_AXIS));

        innerContainer.add(createTextArea());
        innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
        innerContainer.add(createTextArea());
        innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
        innerContainer.add(createTextArea());
        innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
        innerContainer.add(createTextArea());
        innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
        innerContainer.add(createTextArea());
        innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
        innerContainer.add(createTextArea());
        innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));

        return innerContainer;
    }

    private Component createTextArea() {
        JTextArea textArea = new JTextArea();
        textArea.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vitae diam nunc. Proin vulputate, odio consectetur tincidunt aliquet, metus ipsum rhoncus tellus, in dignissim ligula tortor at risus. Aenean rhoncus lorem a magna luctus molestie. Fusce consequat enim vel quam pharetra varius. Vivamus porta condimentum orci non ultrices. Duis magna arcu, fringilla ut eleifend a, volutpat ac elit. Sed ligula enim, dictum id fringilla vitae, ornare vel nulla. Proin lobortis suscipit lectus eget placerat. Morbi aliquam dolor quis lectus tincidunt eu volutpat risus fermentum. Nunc et sapien a nisl aliquet auctor. Phasellus nec sem tellus, et scelerisque sapien. In a nibh vestibulum velit convallis sodales. Vestibulum tempor fringilla vulputate. Duis hendrerit dolor id urna aliquam cursus auctor enim pulvinar. Nulla rutrum fringilla eros, id congue nisi mattis in. Pellentesque consectetur eleifend mauris, ut aliquam purus convallis ac. Vestibulum pretium, sem at congue faucibus, leo leo volutpat odio, ut feugiat nulla felis a diam. Praesent dignissim eros ac eros semper auctor. Phasellus eu sapien nibh. Quisque pulvinar tristique lectus, quis porttitor purus suscipit sed. Curabitur gravida, ipsum ut vehicula aliquet, erat ipsum tincidunt nulla, eget bibendum felis dolor vel risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus vitae nisl odio, blandit laoreet nibh.");
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        return textArea;
    }

}

每当您水平调整框架大小时,只要框架变小,文本就会消失。出现一个水平滚动条,但因为:

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

文本消失了.. JTextArea 应相应调整

4

4 回答 4

20

好的,所以问题是 JPanel 不喜欢嵌入到 JScrollPane 中。

为了让一个组件与 JScrollPane “玩得很好”,它需要实现 Scrollable。如果您只是将 JTextArea 添加到 JScrollPane 中,它将完全按照您的需要工作。

您需要创建一个小的自定义类来扩展 JPanel 并实现 Scrollable,而不是使用 JPanel 来保存所有 JTextAreas。

我重新编写了您的示例并将其粘贴在此处:http ://pastebin.com/q9x4fv3H ,以便您可以查看所有代码。可滚动的 JPanel 应如下所示:

private static class ScrollablePanel extends JPanel implements Scrollable{
    public Dimension getPreferredScrollableViewportSize() {
        return super.getPreferredSize(); //tell the JScrollPane that we want to be our 'preferredSize' - but later, we'll say that vertically, it should scroll.
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 16;//set to 16 because that's what you had in your code.
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 16;//set to 16 because that's what you had set in your code.
    }

    public boolean getScrollableTracksViewportWidth() {
        return true;//track the width, and re-size as needed.
    }

    public boolean getScrollableTracksViewportHeight() {
        return false; //we don't want to track the height, because we want to scroll vertically.
    }
}

干杯!

于 2013-04-03T12:08:33.917 回答
1

您应该为您添加一个视口scrollPane

scrollPane.setViewportView(textArea)
于 2013-04-03T09:48:55.633 回答
0

采用

txtArea.setWrapStyleWord(true);
txtArea.setLineWrap(true);

它包装文本并避免水平滚动窗格。

并尝试 GridBagLayout。使用 weightx 和 weighty。它将处理调整大小问题

于 2013-04-03T09:17:29.503 回答
0

启用换行时 JTextArea 永远不会缩小

我在带有 Gridlayout 的 JPanel 中有一个 JTextArea 有这个问题。通过简单地设置 TextArea 最小大小来解决我的问题。

textArea.setMinimumSize(new Dimension(20,40));
于 2017-04-11T13:30:11.680 回答