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

扶醉桌前 提交于 2019-12-20 02:56:19

问题


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?


回答1:


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




回答2:


You can use the setFont method. For your example try :

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

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



来源:https://stackoverflow.com/questions/8515477/how-to-stylize-make-bold-text-in-a-jtextarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!