JLabel - 将更长的文本显示为多行? [英] JLabel - Show longer text as multiple lines?
问题描述
所以说我想要在 JLabel
中显示一条非常长的行。我该怎么办?
So say I have a really long line that I want to display in a JLabel
. How can I do it?
目前,更长的行显示为:
Currently, longer lines come up as this:
我必须调整窗口大小以查看完整文本。
I have to resize the window to see the complete text.
如果文本差不多达到我的 JFrame
的宽度,我怎么能这样做呢?
How can I make it so that there's linebreaks when the text almost reaches the width of my JFrame
?
我不确定这里是否需要任何代码才能回答这个问题,但仍然是:
I'm not sure if any code is required here for you to answer this, but still:
我的框架属性:
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(450, 400));
frame.setLocation(new Point(400, 300));
frame.setLayout(new BorderLayout());
我要修改的标签:
question = new JLabel("Question:");
question.setFont(new Font("Serif", Font.BOLD, 15));
question.setHorizontalAlignment(JLabel.CENTER);
编辑:
更多详情:
More details:
我正在从文件中读取行,然后显示它们。线条的大小不固定,所以我不知道在哪里放< br>
at。
I am reading lines from a file and then displaying them. The size of lines is not fixed, and so I do not know where to put <br>
at.
编辑2:
我最终使用 JTextArea
。
private JTextArea textAreaProperties(JTextArea textArea) {
textArea.setEditable(false);
textArea.setCursor(null);
textArea.setOpaque(false);
textArea.setFocusable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
return textArea;
}
推荐答案
再举一个例子,显示出来,使用正确的布局管理器,包含在 HTML
标签中的文本将自动换行到可用空间...
Just another example, showing that, with the right layout manager, text wrapped in HTML
tags will automatically wrap to the available space...
public class TestHTMLLabel {
public static void main(String[] args) {
new TestHTMLLabel();
}
public TestHTMLLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
StringBuilder sb = new StringBuilder(64);
sb.append("<html>I have something to say, it's beter to burn out then to fade away.").
append(" This is a very long String to see if you can wrap with in").
append("the available space</html>");
JLabel label = new JLabel(sb.toString());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(label);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这篇关于JLabel - 将更长的文本显示为多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!