我想实现以下目标:
+------------------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都不会进行相应的调整。因此会出现一个水平滚动条,但我想避免出现这种情况。我希望用户只能垂直滚动。
有人有什么好的建议吗?谢谢
编辑:
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应进行相应调整