我想实现以下目标:
+------------------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 应相应调整