gpt4 book ai didi

java - 使用 JScrollPane 缩小 JPanel

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:52 31 4
gpt4 key购买 nike

是否有任何关于需要什么黑魔法才能使 JTextAreaS 正确缩小到它们在调整大小时已经正确增长的方式的提示?我尝试了设置首选大小和在各种事物上实现 Scrollable 的各种组合,如不同的文档和其他 SO 答案所示,但没有看到任何变化。

“额外的”JPanelS 不是垃圾,我只是删除了其中的其他内容以生成更具可读性的测试用例:

public class WeirdShrinking {
public static void main(String[] args) {
JDialog jDialog = new JDialog((JFrame) null, true);

JPanel content = (JPanel) jDialog.getContentPane();

JPanel wrapper = new JPanel();
wrapper.setLayout(new MigLayout("wrap 1", "[grow]", ""));

for (int i = 0; i < 5; i++) {
JPanel panel = new JPanel();
panel.setLayout(new MigLayout("", "[grow]", ""));
JTextArea description = new JTextArea();
description.setEditable(false);
description.setOpaque(false);
description.setLineWrap(true);
description.setWrapStyleWord(true);
description.setFont(new Font("Arial", Font.PLAIN, 12));
description
.setText("Lorem ipsum ddsfolor sit amet, consectetur adipiscing elit. Curabitur viverra vehicula fermentum. Sed ac libero ut massa aliquam ornare. Nunc porttitor interdum turpis, porta viverra purus aliquam quis. In dignissim faucibus nunc, non iaculis sapien. In rutrum eleifend pharetra. Aliquam velit dui, pulvinar ut est ut, sagittis congue ligula. Etiam tincidunt varius consequat.");
panel.add(description, "growx");
wrapper.add(panel, "growx, wrap");
}

content.add(new JScrollPane(wrapper, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));

jDialog.setSize(300, 200);
jDialog.setVisible(true);
}
}

最佳答案

经过一些研究和大量的试验和错误,我终于通过结合其他线程的两个解决方案解决了我的问题:

  • 强制容器面板报告不受增长影响的最小尺寸
  • 为 MigLayout 提供最小列宽

为了让将来遇到此问题的人也能够解决它,我将代码发布到上述示例的工作版本中,并标记了两个关键更改:

public class WeirdShrinking {
public static void main(String[] args) {
JDialog jDialog = new JDialog((JFrame) null, true);

JPanel content = (JPanel) jDialog.getContentPane();
content.setLayout(new MigLayout("wrap 1", "[grow]", ""));

JPanel wrapper = new JPanel();
wrapper.setLayout(new MigLayout("wrap 1", "[grow]", ""));

for (int i = 0; i < 5; i++) {
// *** Change 1, this obviously should be done in a proper class ***
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(1, 1);
}
};
panel.setLayout(new MigLayout("", "[grow]", ""));
JTextArea description = new JTextArea();
description.setEditable(false);
description.setOpaque(false);
description.setLineWrap(true);
description.setFont(new Font("Arial", Font.PLAIN, 12));
description
.setText("Lorem ipsum ddsfolor sit amet, consectetur adipiscing elit. Curabitur viverra vehicula fermentum. Sed ac libero ut massa aliquam ornare. Nunc porttitor interdum turpis, porta viverra purus aliquam quis. In dignissim faucibus nunc, non iaculis sapien. In rutrum eleifend pharetra. Aliquam velit dui, pulvinar ut est ut, sagittis congue ligula. Etiam tincidunt varius consequat.");
// *** Change 2 ***
panel.add(description, "growx, wmin 10");
wrapper.add(panel, "growx, wrap");
}

content.add(new JScrollPane(wrapper, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER), "growx");

jDialog.setSize(300, 200);
jDialog.setVisible(true);
}
}

关于java - 使用 JScrollPane 缩小 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788540/

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