单击JButton Java时更改字体样式 [英] Changing Font Style when Clicking on a JButton Java

查看:171
本文介绍了单击JButton Java时更改字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当点击 JButton 时,如何改变 Font STYLE code>?

我想要有3个按钮,每个都将样式更改为 PLAIN BOLD ITALIC



我已阅读字体Class API 但我没有像 setStyle 我们只能 getStyle



我发现在java中的字体类是比它应该更复杂的:S。

您需要调用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆