单击JButton Java时更改字体样式 [英] Changing Font Style when Clicking on a JButton Java
问题描述
当点击 我想要有3个按钮,每个都将样式更改为 我已阅读 我发现在java中的字体类是比它应该更复杂的:S。 JButton 时,如何改变
Font
的 STYLE
code>?
PLAIN
或 BOLD
或 ITALIC
字体Class API
但我没有像 setStyle
我们只能 getStyle
setFont(...)
不是setStyle。
例如,如果要保留相同的字体,但改变一个名为字段的JTextField风格,你可以做这样的事情:
field.setFont(field。 。的getFont()的deriveFont(Font.BOLD));
编辑
设定字体大胆和斜体,你会或位图:
field.setFont(field.getFont()。deriveFont(Font.BOLD | Font.ITALIC));
请注意,这使用了使用单个管道符号的按位包含OR运算符: |
而不是使用双管道符号的逻辑OR运算符: ||
。
另外请注意,如果进一步细微和混乱,可以使用 |
作为逻辑OR运算符,但通常更喜欢使用 |这是因为后者是一个短路运算符,因为如果表达式的左边是真的,那么右边甚至没有被计算。
How to change the STYLE
of the Font
when clicking on a JButton
?
I'm trying to have 3 buttons each change styles to PLAIN
or BOLD
or ITALIC
I've read the font Class API
but I there is nothing like setStyle
we can only getStyle
I find font class in java is quite complicated more than it should :S.
You would need to call setFont(...)
not setStyle.
For example, if you want to keep the same font but change the style of a JTextField called "field" you could do something like:
field.setFont(field.getFont().deriveFont(Font.BOLD));
Edit
To set the font to both bold and italic, you'd or the bitmaps:
field.setFont(field.getFont().deriveFont(Font.BOLD | Font.ITALIC));
Please note that this uses the bitwise inclusive OR operator which uses a single pipe symbol: |
rather than the logical OR operator which uses a double pipe symbol: ||
.
Also note for further subtlety and confusion that |
can be used as a logical OR operator, but you'll usually prefer to use ||
for this since the latter is a "short-circuit" operator in that if the left hand side of the expression is true, the right hand side isn't even evaluated.
这篇关于单击JButton Java时更改字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!