设置Swing程序的默认字体
我想知道如何为我的整个 Java swing 程序设置默认字体。根据我的研究,它似乎可以通过 UIManager
来完成,与 LookAndFeel
有关,但我找不到具体的操作方法,并且 UIManager
看起来相当复杂。
I was wondering how to set the default font for my entire Java swing program. From my research it appears it can be done with UIManager
, something to do with LookAndFeel
, but I can't find specifically how to do it, and the UIManager
appears pretty complicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
尝试:
通过...调用
try:
Call by ...
来源:http://www.jguru.com/faq/view.jsp?EID =340519
Source: http://www.jguru.com/faq/view.jsp?EID=340519
这不仅会在您的完整 UI 上设置 Tahoma,还会打开抗锯齿功能,使任何字体立即变得更加美观。
This will not only set Tahoma on your complete UI but also turn on anti-aliasing which makes any font much more beautiful immediately.
我认为这更好,为当前的 laf 而不是整个 UIManager 调用它
在实例化 JFrame 对象之前,将其放在
main 中的某个位置。
它对我来说非常有效。
请记住,对于没有指定字体的组件,这是默认字体。
来源:http://www.java.net/node/680725
I think this is better, calling it for the current laf instead of the whole UIManager
put this
Somewhere in the main before instantiating your JFrame object.
It worked perfectly for me.
Remember this is the default font, for the components that have no specified font.
source: http://www.java.net/node/680725
受 Romain Hippeau 的启发,如果您只想设置字体大小,请使用此代码。
Inspired by Romain Hippeau, use this code if you want to set just the font size.
请注意,设置默认字体的方式取决于您使用的外观和感觉。
Romain Hippeau 描述的解决方案适用于许多 LAF,但不适用于 Nimbus。 sherif 发布的版本适用于 Nimbus,但不适用于其他版本(例如 Metal)。
将两者结合起来可以适用于大多数 LAF,但这些解决方案都不适用于 GTK+ LAF。
我认为(但我不确定),没有跨平台的解决方案。
Be aware that the way to set the default font depends on the Look And Feel you're using.
The solution described by Romain Hippeau works fine with a lot of LAF but not with Nimbus. The one posted by sherif works fine with Nimbus, but not with others (Metal, for instance).
Combining both could work on most of LAF, but none of these solutions works with GTK+ LAF.
I think (but I'm not sure), there's no cross-platform solution.
的完整按键列表
作为 @Amir 答案的完成,这是我使用此函数
,并在调用 ui 之前在
main
中调用它。有关 Swing UI Manager 键的完整列表 检查这个链接
As a completion of @Amir answer, this is the complete list of keys
I use this function
and i call it in
main
before invoking the uiFor a complete list of Swing UI Manager keys check this link
正确的答案是 Amir Raminfar 给出的答案,但您必须将字体封装为 FontUIResource。
例如:
The correct answer is the one given by Amir Raminfar but you have to encapsulate the font as a FontUIResource.
For example:
我正在使用 Nimbus L&F。
使用 @Romain Hippeau 的代码,我必须使用
UIManager.getLookAndFeelDefaults()
而不是UIManager.getDefaults()
并使用返回的引用来put
修改值:由于某种原因,它似乎不适用于默认的 L&F...(基于我执行的有限测试)
I'm using Nimbus L&F.
Using code from @Romain Hippeau, I had to use
UIManager.getLookAndFeelDefaults()
instead ofUIManager.getDefaults()
and use the returned reference toput
modified values:For some reason, it does not seem to work with the default L&F... (based on the limited tests I performed)
为了解决这个问题,我只需实现 AWTEventListener 并监听 ContainerEvent 的 COMPONENT_ADDED 。
所有故事描述位于:http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language
所有代码位于:<一href="https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip" rel="nofollow">https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip
AWTEventListener
)
To solve this problem, I just implement AWTEventListener and listen for COMPONENT_ADDED of ContainerEvent.
All story description at: http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language
All code at: https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip
我使用了 Synth 外观 XML 文件并定义了那里有字体。简单、灵活、大陆。
您需要创建一个带有
createFont
的类,例如:在您的合成 xml 中定义字体,例如:
然后您可以在 xml 中的任何位置使用它作为参考。
I used the Synth look and feel XML file and defined the fonts there. Easy, flexible and continent.
You need to create a class with a
createFont
like:And in your synth xml define the font like:
then you may use it as a reference wherever you want in the xml.
这些解决方案都不适合我,我构建了自己的(愚蠢的)解决方案,但它有效:
None of these solutions work fine for me, I built my own (stupid) one but it works: