Is there any way to get the system default font name in Java? The default font can differ from os. So it can create trouble if we use font Arial and the jar is running in Linux w开发者_JS百科ithout having Arial font installed.
Try this:
private final Font FONT = new JLabel().getFont();
JavaFX makes this a lot easier:
import javafx.scene.text.Font;
then use:
Font defaultFont = Font.getDefault();
or
// Where 14 is the font size
Font defaultFont = new Font(14);
Use the defined Font
constants such as SERIF
/SANS_SERIF
etc.
I am currently using this to get the default font, although I would rather not need to use a graphics object to get it:
private final Font getFont()
{
Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics();
Font font = new Font(g.getFont().toString(), 0, 12);
g.dispose();
return font;
}
I don't think there is a way of retrieving a system default font(in Swing/AWT the font is normally associated with the current LAF and component, for instance), but if your concern is font compatibility - you could check the font you are using against all the system fonts:
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = e.getAllFonts();
and make a "fail-over" choice if it doesn't exist.
Take a look at public static Font decode(String str) here. When the decode method receives a null pointer as a parameter it returns the "Dialog" font which is usually the system font.
getFont() returns the current font, which is (usually?) the default. I did this to increase font size.
public MyTextArea(){
Font currentFont = super.getFont();
String fontName = currentFont.getFontName();
int fontStyle = currentFont.getStyle();
int fontSize = currentFont.getSize() + 4;
super.setFont(new Font(fontName, fontStyle, fontSize));
}
In Windows, Segoe UI
Visit http://www.apaddedcell.com/sites/www.apaddedcell.com/files/fonts-article/final/index.html to see a list of preinstalled fonts.
I chose Verdana
精彩评论