我有一个jpanel,它在jscrollpane中使用formlayout。
由于某种原因,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的SetMaximums有关,但我想拥有这些组件的最大大小:)
这与布局无关,并且由于您的限制在此处限制组件的大小:
JPanel container = new JPanel();
container.setPreferredSize(new Dimension(10, 10)); // *****
scrollPane_4.setViewportView(container);
通过调用setPreferredSize(...)
,您的容器JPanel不会大于该尺寸。解决方案:不要这样做,不要致电setPreferredSize(...)
。要么将您的组件大小达到其自然的首选大小,取决于其所保留的组件,要么覆盖其getPreferredSize()
方法以返回大小有意义的方法,具体取决于GUI的状态。