开发者

Can we combine 2 font styles together in Java?

I am trying to change the font of a JLabel so it is both BOLD and ITALIC, but it seems there's no static field defined to do so. How can we combine two styles so we can have a bold, italic font?

This code will do i开发者_JAVA技巧t with just bold by using the static field BOLD, but there's no field defined for both bold and italic:

Font font = new Font("Verdana", Font.BOLD, 12);
label = new JLabel ("New Image") ;
label.setFont(font);
label.setForeground(Color.Gray.darker());


Yes, the style parameter is seen as a bitmask:

new Font("Verdana", Font.BOLD | Font.ITALIC, 12)


From the API documentation of this constructor:

Parameters:

  • ...
  • style - the style constant for the Font. The style argument is an integer bitmask that may be PLAIN, or a bitwise union of BOLD and/or ITALIC (for example, ITALIC or BOLD|ITALIC). If the style argument does not conform to one of the expected integer bitmasks then the style is set to PLAIN.
  • ...

Thus, use

new Font("Verdana", Font.BOLD | Font.ITALIC, 12);

here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜