如何使字符串中的某些字母变粗
How to make some letters in string BOLD
我正在寻找一种简单的方法来将标签中的某些字母设为粗体。
我有一个这样的字符串:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
和一个标签:
Label s = new Label(r);
我需要将 "y" 和 "sin(x), cos(x)" 设为粗体。我尝试使用 HTML,但它没有用(也许我用错了)。如果我为该标签设置字体,那么整个标签都是粗体(包括那些输出),这不是我需要的。
您可以在 JLabel 和其他一些 Java 组件中使用 html 样式。如果您的文本以 <html>
开始并以 </html>
结束,那么将呈现 JLabel 中的 html 代码。
这应该可以解决您的问题:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
尝试使用 HTML(字符串必须以 <html>
开头并以 </html>
结尾):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
说明! <html>
告诉 Java 渲染 HTML(超文本标记语言),<strong>
告诉 HTML 里面的文本非常重要,通常用粗体表示.
我正在寻找一种简单的方法来将标签中的某些字母设为粗体。 我有一个这样的字符串:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
和一个标签:
Label s = new Label(r);
我需要将 "y" 和 "sin(x), cos(x)" 设为粗体。我尝试使用 HTML,但它没有用(也许我用错了)。如果我为该标签设置字体,那么整个标签都是粗体(包括那些输出),这不是我需要的。
您可以在 JLabel 和其他一些 Java 组件中使用 html 样式。如果您的文本以 <html>
开始并以 </html>
结束,那么将呈现 JLabel 中的 html 代码。
这应该可以解决您的问题:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
尝试使用 HTML(字符串必须以 <html>
开头并以 </html>
结尾):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
说明! <html>
告诉 Java 渲染 HTML(超文本标记语言),<strong>
告诉 HTML 里面的文本非常重要,通常用粗体表示.