小智 27
试试这个:
private final Font FONT = new JLabel().getFont();
Run Code Online (Sandbox Code Playgroud)
Sup*_*tar 11
JavaFX使这更容易:
import javafx.scene.text.Font;
Run Code Online (Sandbox Code Playgroud)
然后使用:
Font defaultFont = Font.getDefault();
Run Code Online (Sandbox Code Playgroud)
要么
// Where 14 is the font size
Font defaultFont = new Font(14);
Run Code Online (Sandbox Code Playgroud)
我目前正在使用它来获取默认字体,虽然我宁愿不需要使用图形对象来获取它:
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;
}
Run Code Online (Sandbox Code Playgroud)