javascript 如何在 JTextArea 中风格化(加粗)文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8515477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:40:51  来源:igfitidea点击:

How to stylize (make bold) text in a JTextArea?

javascriptswingjtextarea

提问by shree

JTextArea txt = new JTextArea();
    StringBuffer sb = new StringBuffer();
    sb.append("Employee Information");
    sb.append("\n");
    sb.append("Name:");
    txt.setText(sb.toString());

Here I need to set "Employee Information"to BOLD format..How to do that.. I tried like this

在这里,我需要将“员工信息”设置为粗体格式..怎么做..我试过这样

 sb.append("<html><b>Employee Information</b></html>");

But its printing the text normally... how to make bold?

但它正常打印文本......如何使粗体?

回答by CloudyMarble

The JText Area is a plain text area, for styled text areas you need something like JEditorPaneor JTextPane, take a look at the Documentation

JText 区域是纯文本区域,对于样式文本区域,您需要JEdi​​torPane或 JTextPane 之类的东西,请查看文档

回答by Abhishek Bhandari

You can use the setFontmethod. For your example try :

您可以使用该setFont方法。对于您的示例,请尝试:

Font font = txt.getFont();  
txt.setFont(font.deriveFont(Font.BOLD));

(Keep in mind that txtis the JTextArea, not your actual text.)

(请记住,这txtJTextArea,而不是您的实际文本。)