使用Java开发跨平台,字体在不同平台上的缩放比例不同

问题描述:

我正在为我的大学制作一些软件. GUI是必需的.在它的第一个版本中,我使用了系统外观(它看起来像Linux/Mac/Windows中的本机应用程序).我发现这很麻烦,因为我必须根据操作系统将所有JLabel设置为不同的大小(无论分辨率/像素密度/等等如何).

I am making some software for my university. A GUI is required. In the first revision of it, I had it use the System Look and Feel (so it would look like a native application within Linux/Mac/Windows). I found this to be cumbersome as I had to make all of my JLabels different sized based on the OS (regardless of resolution/pixel density/etc).

完成此操作后,我感觉好吧,我不会再经历这个了",所以我决定不使用System外观并尝试Nimbus.在我测试过的平台上看起来不错,但是JLabel/JTextField字体大小仍然无法在多个平台上正确显示,无论它们是否指定(如Sans Serif 12).有没有一种方法可以使Font真正在所有平台上通用并且避免进行所有此类跨平台测试?

After doing this I was like "okay, I'm NOT going to go through with this again" so I decided to not use the System look and feel and try out Nimbus. It looks nice on the platforms I've tested but the JLabel/JTextField font sizes still don't appear correctly on multiple platforms, regardless of them being specified (as Sans Serif 12). Is there a way I could make the Font truly universal across all platforms and avoid having to do all of this cross-platform testing?

此处所示,使用适当的布局管理器pack()将允许组件利用其首选组件大小,与平台和字体无关.

As shown here, using an appropriate layout manager and pack() will allow a component to leverage it's preferred size, regardless of platform and font.