我有一个在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:
根据“Hovercraft”的提示,如果我注释掉setPreferredSize行,滚动条似乎有效,但引入了另一个问题。布局变得超宽(在屏幕外)以及更多。这可能与row1和row2的setMaximumSize有关,但我希望这些组件的最大大小:)
答案 0 :(得分:2)
这与布局无关,所有这些都归功于您在此限制组件JPanel的大小:
JPanel container = new JPanel();
container.setPreferredSize(new Dimension(10, 10)); // *****
scrollPane_4.setViewportView(container);
通过调用setPreferredSize(...)
您的容器,JPanel不会超过该大小。解决方案:不要这样做,不要拨打setPreferredSize(...)
。要么将组件大小设置为依赖于它所拥有的组件的自然首选大小,要么覆盖其getPreferredSize()
方法以返回有意义的大小,具体取决于GUI的状态。