如何在Java中将字体设置为字符串 [英] how set font to string in Java

查看:142
本文介绍了如何在Java中将字体设置为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个字符串,现在如何设置字体和颜色,



ex:String aa =你好汤姆。 =>我想要字符串aa有颜色是reb和字体:粗体





提前感谢

解决方案

在Component上设置字体,显示字符串。

对于Swing,它将是:



< pre lang =java> private void createLabel(){
JLabel label = new JLabel( Label);
label.setFont( new 字体( Arial,Font.BOLD, 12 ));
label.setForeground(Color.RED);
.add(label);
}





有一个很棒的教程@ oracle.com关于如何使用这些东西:



课程:使用Swing组件 [ ^ ]



我建议将该页面加入书签。你会在那里找到几乎所有GUI所需的东西。


字符串没有字体,句号。



当您在屏幕上显示字符串时,您正在使用一个或另一个UI库来选择字体。



-SA


如果您使用的是applet,那么:

public void onPaint(Graphics g){
g.setFont( new 字体( Arial,Font.BOLD, 12 ));
g.drawString( Hello World! 50 50 );

// 代替Hello World!你可以传递任何字符串
// 50,50是x和y坐标
}


Hi all,
I have a string, now how do I set font and color to it,

ex: String aa = "hello Tom." => I want string aa has color is reb and font: bold


thanks in advance

解决方案

You set the font on the Component, that presents the String.
For Swing that would be:

private void createLabel(){
  JLabel label = new JLabel("Label");
  label.setFont(new Font("Arial", Font.BOLD, 12));
  label.setForeground(Color.RED);
  this.add(label);
}



There is a fantastic tutorial @ oracle.com about how to use those things:

Lesson: Using Swing Components[^]

I recommend to bookmark that page. You''ll find nearly everything that it needs for a GUI there.


Strings do not have fonts, period.

When you present string on screen, you are using one or another UI library where you can choose fonts.

—SA


If you are using applet then:

public void onPaint(Graphics g){
    g.setFont(new Font("Arial", Font.BOLD, 12));
    g.drawString("Hello World!",50,50);

    //In place of Hello World! you can pass any string
    //50,50 are x and y coordinates
}


这篇关于如何在Java中将字体设置为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆