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 di
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.
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?.
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
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:
Windows 7:
Ubuntu Linux: