需要什么样的黑魔法才能使JTextAreaS
在调整大小时适当缩小的方式缩小?我尝试了设置首选大小和实现Scrollable
的各种组合的各种组合,如不同的文档和其他SO答案所示,但没有看到任何变化。
'extra'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);
}
}
答案 0 :(得分:0)
经过一些研究和大量的反复试验,我终于通过结合其他线程的两个解决方案解决了我的问题:
为了让将来有这个问题的人能够解决这个问题,我将代码发布到上面示例的工作版本中,并标记了两个关键更改:
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);
}
}