Java swing BasicTextFieldUI绘画问题

时间:2019-02-05 18:41:43

标签: java swing

我正在一个项目中,我需要更改JTextFields style 。我扩展了 BasicTextFieldUI,并覆盖了paintSafely方法,但是,当绘制组件时,它会绘制 border (即使边框设置为null)。我尝试查看BasicTextUI类的内部,看能画出什么边界的东西,但是我什么也没找到。

问题,

为什么此代码将在组件周围创建边框?

这是我的代码:

public class CustomTextField extends BasicTextFieldUI{

    int borderThickness, edgeRoundness;

    @Override
    protected void paintSafely(Graphics g) {

        JComponent c = (JComponent)this.getComponent();
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(c.getBackground());

        borderThickness = 2;
        edgeRoundness = 20;

        g2.setColor(c.getForeground());
        g2.fillRoundRect(0, 0, c.getWidth(), c.getHeight(), edgeRoundness+5, edgeRoundness+5);
        g2.drawRoundRect(0, 0, c.getWidth(), c.getHeight(), edgeRoundness+5, edgeRoundness+5);

        g2.setColor(c.getBackground());
        g2.fillRoundRect(borderThickness, borderThickness, c.getWidth()-(int)(borderThickness*2.5), c.getHeight()-(int)(borderThickness*2.5), edgeRoundness, edgeRoundness);
        g2.drawRoundRect(borderThickness, borderThickness, c.getWidth()-(int)(borderThickness*2.5), c.getHeight()-(int)(borderThickness*2.5), edgeRoundness, edgeRoundness);


        super.paintSafely(g);
    }

}

可能是什么错误?还有其他方法可以用来绘制组件吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

我在这篇文章中找到了答案!看来,通过将边框设置为null,我对PLAF说过要使用它自己的边框。 Swing JTextField how to remove the border?