gpt4 book ai didi

java - 如何在 JLabel 中换行文本?

转载 作者:行者123 更新时间:2023-12-01 21:13:05 26 4
gpt4 key购买 nike

我有显示动态文本的 JLabel。该文本可以很长也可以很短。我想换行,我正在尝试这样:

    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.setMaximumSize(new Dimension(500, 150));
....
lblInfo=new JLabel();
lblInfo.setText("<html><b>Q: "+ infoObj.getText()+"</b></html>");
...
panel1.add(lblInfo);

但这似乎不起作用。当出现长文本时,这个 JLabel 就会超出屏幕(超出我的面板大小),我只能看到它的结尾。

我在 Stack Overflow 上找到了一些使用 JTextField 而不是标签的解决方案。但由于我的项目中的一些要求,我必须在我的案例中使用 JLabel 本身。

最佳答案

When a long text comes, this JLabel just goes out of the screen

是的,只有当您有 <br> 时,文本才会换行。在实际的文本中。 <br>在文本开头什么也不做。

I found some solutions on stackoverflow using JTextField instead of label

我对此表示怀疑。一个JTextField始终在单行上显示文本。

您在论坛中找到的建议是使用 JTextArea带包装:

JTextArea textArea = new JTextArea(5, 20);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

关于java - 如何在 JLabel 中换行文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40811364/

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