如何使用Font类设置字符串的字体大小,样式在Java中? [英] How to set String's font size, style in Java using the Font class?

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

问题描述

假设我有一个字符串Hello World。我想把这个字符串的样式改成BOLD字体,并把所有字符的大小设置为12到18 [pt]。之后,我想在 JLabel JButton 中使用这个字符串。我可以做什么?

解决方案

请看这里 http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont%28float %29



JComponent有一个setFont()方法。例如

 <$ c  

$ c> JButton b = new JButton();
b.setFont(b.getFont()。deriveFont(18.0f));


Suppose I have a String, "Hello World". I want to change the style of this string to a BOLD font, and set the size of all the characters from 12 to 18 [pt]. After that I want to use this string in a JLabel and JButton. How can I do that?

解决方案

Look here http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont%28float%29

JComponent has a setFont() method. You will control the font there, not on the String.

Such as

JButton b = new JButton();
b.setFont(b.getFont().deriveFont(18.0f));

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

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