JLabel-将较长的文本显示为多行?

所以说我要在一行中显示很长的一行JLabel。我该怎么做?


当前,更长的行出现了:


在此处输入图片说明


我必须调整窗口大小才能查看全文。


如何使文本几乎达到我的宽度时出现换行符JFrame?


我不确定这里是否需要任何代码来回答这个问题,但是仍然:


我的镜框属性:


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);

编辑:更多详细信息:


我正在从文件中读取行,然后显示它们。线的大小不是固定的,所以我不知道放在哪里<br>。


编辑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;

}


墨色风雨
浏览 894回答 3
3回答

大话西游666

另一个例子,表明使用正确的布局管理器,HTML标签中包裹的文本将自动包装到可用空间...在此处输入图片说明public class TestHTMLLabel {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new TestHTMLLabel();&nbsp; &nbsp; }&nbsp; &nbsp; public TestHTMLLabel() {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder(64);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append("<html>I have something to say, it's beter to burn out then to fade away.").&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; append("&nbsp; This is a very long String to see if you can wrap with in").&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; append("the available space</html>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JLabel label = new JLabel(sb.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Test");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLayout(new BorderLayout());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(label);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(100, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}

慕的地6264312

使用HTML在标签内显示文本。JLabel fancyLabel = new JLabel("<html>Punch Taskmaster</html>");(添加了Taskmaster建议的示例)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java