java Swing 中的什么字体在所有操作系统中看起来都一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7187989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What font in Swing looks the same in all OS?
提问by Vigneshwaran
I use Netbeans 7.0 with JDK6 under Windows 7 to design the user interface of my Java application. I apply System look and feel. But it looks the way I want in Windows but differs in MacOS and even worse, it looks different in different window managers in Linux (LXDE, GNOME, KDE, XFCE).
我在 Windows 7 下使用带有 JDK6 的 Netbeans 7.0 来设计我的 Java 应用程序的用户界面。我应用系统外观。但它在 Windows 中看起来是我想要的,但在 MacOS 中有所不同,更糟糕的是,它在 Linux(LXDE、GNOME、KDE、XFCE)中的不同窗口管理器中看起来不同。
By different I mean the fonts look and their size. In Windows, if a label looks "v 1.23", it looks like "v ..." in other OSes because the fonts become bigger in that OS and the JLabel do not have enough place to show. This happens in several places.
我所说的不同是指字体的外观和大小。在 Windows 中,如果一个标签看起来是“v 1.23”,那么它在其他操作系统中看起来就像是“v ...”,因为该操作系统中的字体变大了,而 JLabel 没有足够的地方可以显示。这发生在几个地方。
I don't want to increase the label width. I want the label to look the same in that given width in all OS. By default, Netbeans uses the font Tahoma 11pt on my pc. I think it's not available in all OSes so the other OSes use different font.
我不想增加标签宽度。我希望标签在所有操作系统中的给定宽度下看起来都一样。默认情况下,Netbeans 在我的电脑上使用 Tahoma 11pt 字体。我认为它并非在所有操作系统中都可用,因此其他操作系统使用不同的字体。
Is Arial a common font?
Arial 是一种常见的字体吗?
Should I change the font of every element to Arial manually? Or any other options?
我应该手动将每个元素的字体更改为 Arial 吗?或者有其他选择吗?
采纳答案by Anantha Sharma
When it comes to window managers there is a lot more to it than just font size, for instance the width between 2 controls are handled differently in gnome & KDE. when it comes down to using the same font size across all platforms you can use Sans Serif
font, this is available in all the OS'es I've worked with.
当谈到窗口管理器时,它不仅仅是字体大小,例如,在 gnome 和 KDE 中,两个控件之间的宽度处理方式不同。当涉及到在所有平台上使用相同的字体大小时,您可以使用Sans Serif
字体,这在我使用过的所有操作系统中都可用。
it would also be better (if you can't find serif in an OS where you want to run your app) you can download a font (free ones from GNU Free Fonts).
它也会更好(如果您在要运行应用程序的操作系统中找不到衬线),您可以下载字体(来自GNU Free Fonts 的免费字体)。
when it comes to setting the font sizes & the fonts, why don't you try using a theme & set the font there...
you can use the UIManager.setLookAndFeel()
method to change themes.
here is a link on Look & Feel
在设置字体大小和字体时,为什么不尝试使用主题并在那里设置字体...您可以使用该UIManager.setLookAndFeel()
方法更改主题。这是外观和感觉的链接
回答by trashgod
Instead, use a layout managerand a logical font family. This examplewith the Font
below adds 24-point italic serif text to the center of a (default) BorderLayout
to achieve a pleasing result on disparate platforms.
相反,使用布局管理器和逻辑字体系列。这个例子与Font
下面增加了24点斜体衬线文本到(默认)的中心BorderLayout
,以实现在不同平台上一个可喜的结果。
ta.setFont(new Font("Serif", Font.ITALIC, 24));
Mac OS X:
Mac OS X:
Windows 7:
Windows 7的:
Ubuntu Linux:
Ubuntu Linux:
回答by Sergio
An old question, but I've found it because I was looking for a solution. And my solution is: use DejaVu fonts. The Java dialog font looks different in Linux and Windows, but DejaVuSans 12 is very like the dialog font in Linux and looks the same in Windows (in Windows 8.1 at least). My code:
一个老问题,但我找到了它,因为我正在寻找解决方案。我的解决方案是:使用 DejaVu 字体。Java 对话框字体在 Linux 和 Windows 中看起来不同,但 DejaVuSans 12 与 Linux 中的对话框字体非常相似,并且在 Windows 中看起来相同(至少在 Windows 8.1 中)。我的代码:
...
static Font dejaVuSans;
static final String resPath = "<classpath>/resources/"; // no leading "/"
...
public static void main(String[] args) {
/* See:
* https://stackoverflow.com/questions/7434845/setting-the-default-font-of-swing-program
* https://stackoverflow.com/questions/8361947/how-to-get-getclass-getresource-from-a-static-context
*/
dejaVuSans = null;
Font f;
try {
InputStream istream = <ClassName>.class.getClassLoader().getResourceAsStream(
resPath + "DejaVuSans.ttf");
dejaVuSans = Font.createFont(Font.TRUETYPE_FONT, istream);
f = dejaVuSans.deriveFont(Font.PLAIN, 12);
} catch (Exception e) {
System.err.println(e.getMessage());
f = null;
}
if (f == null)
f = new Font("Dialog", Font.PLAIN, 12);
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get (key);
if (value != null && value instanceof javax.swing.plaf.FontUIResource)
UIManager.put (key, f);
}
...
}
Of course, if DejaVuSans.ttf is not embedded in the jar file you can import it at runtime. See How do you import a font?.
当然,如果 jar 文件中没有嵌入 DejaVuSans.ttf,您可以在运行时导入它。请参阅如何导入字体?.
回答by PeterMmm
Lucida Sans Regular is always bundled with Java but I don't think it will solve the problem that some text/labels go out of format. That depends also on the users's screen resulotion.
Lucida Sans Regular 总是与 Java 捆绑在一起,但我认为它不会解决某些文本/标签格式错误的问题。这也取决于用户的屏幕分辨率。