gpt4 book ai didi

java - JScrollPane 无法与 FormLayout 一起使用

转载 作者:行者123 更新时间:2023-12-02 06:05:00 24 4
gpt4 key购买 nike

我有一个 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:

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

最佳答案

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

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

scrollPane_4.setViewportView(container);

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

关于java - JScrollPane 无法与 FormLayout 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com