开发者

What font in Swing looks the same in all OS?

开发者 https://www.devze.com 2023-03-30 21:16 出处:网络
I use Netbeans 开发者_JS百科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

I use Netbeans 开发者_JS百科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).

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.

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.

Is Arial a common font?

Should I change the font of every element to Arial manually? Or any other options?


Instead, use a layout manager and a logical font family. This example with the Font below adds 24-point italic serif text to the center of a (default) BorderLayout to achieve a pleasing result on disparate platforms.

ta.setFont(new Font("Serif", Font.ITALIC, 24));

Mac OS X:

What font in Swing looks the same in all OS?

Windows 7:

What font in Swing looks the same in all OS?

Ubuntu Linux:

What font in Swing looks the same in all OS?


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.

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).

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


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:

...
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?.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消