[英]Font.createFont(..) set color and size (java.awt.Font)
我想使用TTF文件创建一个新的Font对象。 创建一个Font对象非常简单,但我不知道如何设置颜色和大小,因为我找不到它的方法?
InputStream is = new FileInputStream("helvetica.ttf");
Font helvetica = Font.createFont(Font.TRUETYPE_FONT, is);
字体没有颜色; 只有在使用字体时,您才能设置组件的颜色。 例如,使用JTextArea时:
JTextArea txt = new JTextArea();
Font font = new Font("Verdana", Font.BOLD, 12);
txt.setFont(font);
txt.setForeground(Color.BLUE);
根据此链接 ,createFont()方法创建一个点大小为1且样式为PLAIN的新Font对象。 因此,如果要增加Font的大小,则需要执行以下操作:
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));
return font.deriveFont(12f);
好吧,一旦你有了你的字体,就可以调用deriveFont
。 例如,
helvetica = helvetica.deriveFont(Font.BOLD, 12f);
将字体的样式更改为粗体,将其大小更改为12磅。
因为字体没有颜色,所以需要一个面板来制作背景颜色并为JLabel(如果使用JLabel)和JPanel提供前景颜色来制作字体颜色,如下例所示:
JLabel lblusr = new JLabel("User name : ");
lblusr.setForeground(Color.YELLOW);
JPanel usrPanel = new JPanel();
Color maroon = new Color (128, 0, 0);
usrPanel.setBackground(maroon);
usrPanel.setOpaque(true);
usrPanel.setForeground(Color.YELLOW);
usrPanel.add(lblusr);
标签的背景颜色是栗色,带有黄色字体颜色。
要设置字体的颜色,必须首先通过执行以下操作初始化颜色:
Color maroon = new Color (128, 0, 0);
一旦你完成了,你就把:
Font font = new Font ("Courier New", 1, 25); //Initializes the font
c.setColor (maroon); //Sets the color of the font
c.setFont (font); //Sets the font
c.drawString ("Your text here", locationX, locationY); //Outputs the string
注意:1表示字体的类型,可用于替换Font.PLAIN,25表示字体的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.