如何在文本区域中设置文本的一部分粗体?

时间:2015-12-03 23:31:15

标签: java netbeans jtextarea

I want to display the values in bold

我需要将值更改为粗体而不是整个文本。

这是我的代码:

public String displayOrder(){
    String order = "Order " + count + 
            "\nLength of pipe: " + decimal2.format(length / 39.37) + 
            "m" + "\nDiameter of pipe: " + decimal2.format(outerDiameter) + 
            "''" + "\nGrade of pipe: " + grade +
            "\nColour of pipe: " + colour +
            "\nInner insulation: " + convertToStr(innerInsulation) +
            "\nOuter reinforcement: " + convertToStr(outerReinforcement) +
            "\nChemical resistance: " + convertToStr(chemicalResistance) +
            "\nNumber of pipes: " + quantityOfPipes;
    return order;
}

2 个答案:

答案 0 :(得分:0)

我会使用JEditorPane使用html标记:

JEditorPane editorPane = new JEditorPane("text/html", "");
editorPane.setText("Test <b>bold</b>");

你不能用JTextArea做到这一点,你可以更接近改变字体:

textArea.setFont(textArea.getFont().deriveFont(Font.BOLD));

问题是这会改变整个文本的字体。

答案 1 :(得分:0)

我不认为你可以用普通的jtextarea做到这一点,你应该尝试一下 JEdi​​torPane中

int a, b, c;
while(scanf("%d %d %d", &a, &b, &c)==1){
    // do stuff here
}

How to display bold text in only parts of JTextArea?