开发者

Can we combine 2 font styles together in Java?

开发者 https://www.devze.com 2023-03-24 21:31 出处:网络
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?

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

精彩评论

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