简体   繁体   中英

Getting system font properties in Java/Swing

I am working on a Java application using Swing and I want to set up the GUI using the "proper" fonts and font sizes. With proper I mean the fonts (+size) as defined by the user, so that my application will fit right in to the user's screen setup (resolution / font size combination) without having to provide my own custom settings. My app will be used on Windows, MacOS X and Linux.

I've been unable to find how to do that, or whether this is actually possible in the first place. I found some references to getSystemFont but could not find any documentation.

Can anybody help?

EDIT: Eugene suggested that using setLookAndfeel would already take care of that. While this may be correct, this still leaves me with the issue on how to add eg a JTextField to the screen that has the proper height, as I need to pass something on to .setBounds

Actually system look and feel suppose to do it for each platform. You just have to use system look and feel:

UIManager.setLookAndfeel( UIManager. getSystemLookAndFeelClassName())

And do it before your GUI is created.

this still leaves me with the issue on how to add eg a JTextField to the screen that has the proper height, as I need to pass something on to .setBounds

No, you should be using LayoutManagers. They do all the work for you. There is no need to use a null layout and therefore you don't need to set the bounds of the component.

Read the section from the Swing tutorial on Using Layout Managers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM