Can we combine 2 font styles together in Java?

南楼画角 提交于 2019-12-03 16:44:23

问题


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 it 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());

回答1:


Yes, the style parameter is seen as a bitmask:

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



回答2:


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.



来源:https://stackoverflow.com/questions/6913228/can-we-combine-2-font-styles-together-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!