我想实现以下目标:
+------------------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应相应调整
答案 0 :(得分:16)
好的,问题是JPanel不喜欢嵌入到JScrollPane中。
为了使组件与JScrollPane“玩得很好”,它需要实现Scrollable。 如果您刚刚将JTextArea添加到JScrollPane中,它将完全按照您的需要工作。
您需要创建一个扩展JPanel并实现Scrollable的小型自定义类,而不是使用JPanel来保存所有JTextAreas。
我重新编写了您的示例并将其粘贴到此处: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.
}
}
干杯!
答案 1 :(得分:1)
您应为scrollPane
添加一个视口:
scrollPane.setViewportView(textArea)
答案 2 :(得分:0)
使用
txtArea.setWrapStyleWord(true);
txtArea.setLineWrap(true);
它包装文本并避免水平滚动窗格。
试试吧 GridBagLayout.使用weightx和weighty。它将负责调整大小问题
答案 3 :(得分:0)
我在带有Gridlayout的JPanel中有一个JTextArea,但是有这个问题。只需设置TextArea最小尺寸即可解决我的问题。
textArea.setMinimumSize(new Dimension(20,40));