JScrollPane内JPanel上的JTextArea无法正确调整大小

+------------------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());

....等

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

有人有一些好的秘诀吗?谢谢!

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应该相应地进行调整

回答:

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

为了使组件能够使用JScrollPane“很好地播放”,它需要实现Scrollable。如果您只是将JTextArea添加到JScrollPane中,它将完全按照您的要求工作。

无需使用JPanel来保存所有JTextArea,您需要制作一个小的自定义类来扩展JPanel并实现Scrollable。

我已经重写了您的示例并将其粘贴到此处:http :

//pastebin.com/q9x4fv3H,以便您可以查看所有代码。Scrollable

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.

}

}

干杯!

以上是 JScrollPane内JPanel上的JTextArea无法正确调整大小 的全部内容, 来源链接: utcz.com/qa/406594.html

回到顶部