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 bePLAIN
, or a bitwise union ofBOLD
and/orITALIC
(for example,ITALIC
orBOLD|ITALIC
). If the style argument does not conform to one of the expected integer bitmasks then the style is set toPLAIN
.- ...
Thus, use
new Font("Verdana", Font.BOLD | Font.ITALIC, 12);
here.
精彩评论