Java 如何使用当前的外观在swing中绘制字符串?

Java 如何使用当前的外观在swing中绘制字符串?,java,swing,Java,Swing,我正在编写一个定制的JComponent,它将从字符串中提取一些文本 当前,文本图形代码如下所示: @Override public void paintComponent(Graphics graphics) { super.paintComponent(graphics); Rectangle bounds = graphics.getClipBounds(); int maxAscent = graphics.getFontMetrics().getMaxAscen

我正在编写一个定制的
JComponent
,它将从字符串中提取一些文本

当前,文本图形代码如下所示:

@Override
public void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);
    Rectangle bounds = graphics.getClipBounds();
    int maxAscent = graphics.getFontMetrics().getMaxAscent();
    graphics.setColor(Color.BLACK);
    graphics.drawString(text,bounds.x,bounds.y+maxAscent);
}
但是,这会绘制别名文本。因此,我在谷歌上搜索了绘图抗锯齿文本,发现我可以添加以下行:

graphics2D.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
但后来我想。。。我还缺什么?如果用户禁用了抗锯齿文本,并且当前的外观符合这一点,但我的代码不符合这一点,该怎么办


我想按照当前的外观绘制文本。我该怎么做?

您可以使用桌面设置。这里解释如下:

而不是使用

graphics2D.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
使用


您可以使用桌面设置。这里解释如下:

而不是使用

graphics2D.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
使用

并建议这不是一种跨平台方法(即,它不会在所有情况下都有效)。并建议这不是一种跨平台方法(即,它不会在所有情况下都有效)。