0

我有一个在 JScrollPane 中使用 FormLayout 的 JPanel。

出于某种原因,当内容变得太大时,JScrollPane 不会显示滚动条。

我是 Java 新手,所以我很可能遗漏了一些小事:)

到目前为止,这是我的代码。将窗口大小调整为较小的高度应该会导致 JScrollPane 显示滚动条,但事实并非如此。请帮忙!

我知道我应该在另一个线程等中运行 GUI,但这只是一个模型,它不应该影响结果。

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.BorderFactory;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class Test7 extends JFrame {

@SuppressWarnings("deprecation")
public Test7() {

    UIManager.put("swing.boldMetal", Boolean.FALSE);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Test7");
    this.setSize(1000, 700);
    this.setLocationRelativeTo(null);

    JScrollPane scrollPane_4 = new JScrollPane();
    scrollPane_4.setBorder(BorderFactory.createEmptyBorder());
    this.add(scrollPane_4);

    JPanel bottom = new JPanel();
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));

    JPanel container = new JPanel();
    container.setPreferredSize(new Dimension(10, 10));

    scrollPane_4.setViewportView(container);

    FormLayout layout = new FormLayout("default", "fill:default:grow");
    PanelBuilder builder = new PanelBuilder(layout, container);
    CellConstraints cc = new CellConstraints();

    layout.appendRow(new RowSpec("pref"));
    builder.add(bottom, cc.xy(1, layout.getRowCount()));        

    JPanel row1 = new JPanel();
    row1.setBackground(Color.decode("#fcfcfc"));
    row1.setLayout(new BoxLayout(row1, BoxLayout.X_AXIS));
    row1.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    row1.setMaximumSize(new Dimension(700, Short.MAX_VALUE));
    bottom.add(row1);

    JLabel lblAaaa = new JLabel("<html>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</html>");
    lblAaaa.setAlignmentY(JPanel.TOP_ALIGNMENT);
    lblAaaa.setBackground(Color.decode("#f2f2f2"));
    lblAaaa.setOpaque(true);
    lblAaaa.setBorder(new LineBorder(Color.decode("#cccccc"),1));
    row1.add(lblAaaa);

    layout.appendRow(new RowSpec("10px"));        

    JPanel bottom2 = new JPanel();
    bottom2.setOpaque(false);
    bottom2.setLayout(new BoxLayout(bottom2, BoxLayout.Y_AXIS));

    layout.appendRow(new RowSpec("pref"));
    builder.add(bottom2, cc.xy(1, layout.getRowCount()));        

    JPanel row2 = new JPanel();
    row2.setBackground(Color.decode("#fcfcfc"));
    row2.setLayout(new BoxLayout(row2, BoxLayout.X_AXIS));
    row2.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
    row2.setMaximumSize(new Dimension(700, Short.MAX_VALUE));
    bottom2.add(row2);

    JLabel lblAaaa2 = new JLabel("<html>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</html>");
    lblAaaa2.setAlignmentY(JPanel.TOP_ALIGNMENT);
    lblAaaa2.setBackground(Color.decode("#a9dff5"));
    lblAaaa2.setOpaque(true);
    lblAaaa2.setBorder(new LineBorder(Color.decode("#8fb4c2"), 1));
    row2.add(lblAaaa2);

    this.setVisible(true);

}

public static void main(String[] args) {
    new Test7();
}
}

更新 1:

按照“气垫船”的提示,如果我注释掉 setPreferredSize 行,滚动条似乎可以工作,但会引入另一个问题。布局变得超宽(屏幕外)等等。这可能与 row1 和 row2 的 setMaximumSize 有关,但我想要这些组件的最大大小:)

4

1 回答 1

2

这与布局无关,都是因为您在这里限制了组件 JPanel 的大小:

JPanel container = new JPanel();
container.setPreferredSize(new Dimension(10, 10)); // *****

scrollPane_4.setViewportView(container);

通过调用setPreferredSize(...)您的容器 JPanel 不会比该大小更大。解决方案:不要这样做,不要调用setPreferredSize(...). 要么将组件大小设置为它自己的自然首选大小,具体取决于它所拥有的组件,要么覆盖其getPreferredSize()方法以返回一个有意义的大小,具体取决于 GUI 的状态。

于 2014-03-12T21:10:11.483 回答