这是我到目前为止所做的,但该领域的文字没有抗锯齿。我已经尝试谷歌搜索了一段时间,但无法找到讨论它的任何线程(令我惊讶的是)。有谁知道如何做到这一点?
public class SearchField extends JTextField{
public SearchField(){
super();
this.setOpaque(false);
this.setPreferredSize(new Dimension(fieldWidth, fieldHeight));
this.setBorder(new EmptyBorder(4,8,4,8));
this.setFont(fieldFont);
}
public void paintComponent(Graphics paramGraphics){
Graphics2D g = (Graphics2D) paramGraphics;
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(ColorConstants.LIGHT_GRAY);
g.fillRoundRect(0,0,fieldWidth,fieldHeight,4,4);
super.paintComponent(g);
}
}
答案 0 :(得分:1)
我发现使用显示TextLayout
的here会很有帮助,因为人们可以使用isAntiAliased
和usesFractionalMetrics
调整FontRenderContext
。
示例使用BufferedImage
是巧合。
答案 1 :(得分:0)
这是我决定要做的事情,直到找到更优雅的解决方案 - 运作良好。
private class SearchField extends JTextField{
private final int fieldWidth = 375;
private final int fieldHeight = 30;
private final Font fieldFont = FontLoader.getCustomFont("Gotham-Bold.ttf", 15);
private final Color foreground = ColorConstants.SEARCH_FIELD_FOREGROUND;
private final Color background = ColorConstants.SEARCH_FIELD_BACKGROUND;
public SearchField(){
super();
this.setOpaque(false);
this.setPreferredSize(new Dimension(fieldWidth, fieldHeight));
this.setBorder(new EmptyBorder(5,5,5,5));
this.setFont(fieldFont);
this.setForeground(new Color(0,0,0,0));
this.setSelectedTextColor(new Color(0,0,0,0));
}
@Override
public void paintComponent(Graphics paramGraphics){
Graphics2D g = (Graphics2D) paramGraphics.create();
GraphicUtils.enableAntiAliasing(g); //RenderingHints
g.setColor(background);
g.fillRoundRect(0, 0, fieldWidth, fieldHeight, 4, 4);
super.paintComponent(g);
g.setColor(foreground);
g.drawString(this.getText(), 5, 20);
}
}