JLabel 中的文本被复制

Text Being Duplicated in a JLabel

我正在设计一个用于配置托盘的应用程序,我有一个 class 扩展 JLabel,我用它来创建带有旋转文本的 JLabel。我在网上找到了一些关于如何执行此操作的示例,并且我的轮换工作得很好,它并不完美,但它正在进行中。
我现在遇到的问题是我旋转的 JLabel 中的文本是重复的,我不知道为什么。下面的图片显示了每个标签中的重复文本,它在某些标签中比在其他标签中更突出,例如,对于高度标签,可以清楚地看到重复文本。

重复的标签文本图像:

这是我的 RotatableText class 的源代码,它扩展了 JLabel.

public class RotatableText extends JLabel 
{
    private static final long serialVersionUID = 1L;

    private String text;
    private double angle;

    public final static String DEGREES = "deg";
    public final static String RADIANS = "rad";

    private final static double TO_RADIANS = Math.PI/180;
    private final static double TO_DEGREES = 180/Math.PI;

    /**
     * Creates text rotated by angle in a clockwise direction if clockwise is true,
     * or anti-clockwise if it's false
     * @param angle angle to be rotated by in degrees
     * @param clockwise determines direction of rotation@exception Exception if angleUnit is not RotatableText.DEGREES or RotatableText.RADIANS
     */
    public RotatableText(String text, double angle, boolean clockwise, final String angleUnit) throws IllegalAngleUnitException 
    {
        if (!(angleUnit.equals(DEGREES) || angleUnit.equals(RADIANS)))
        {
            throw new IllegalAngleUnitException("Invalid Angle Selected");
        }
        else if (angleUnit.equals(DEGREES))
        {
            if (!clockwise)
            {
                this.angle = -angle * TO_RADIANS;
                super.setText(text);
            }
            else
            {
                this.angle = angle * TO_RADIANS;
                super.setText(text);
            }
        }
        else if (angleUnit.equals(RADIANS))
        {
            if (!clockwise)
            {
                this.angle = -angle;
                super.setText(text);
            }
            else
            {
                this.angle = angle;
                super.setText(text);
            }
        }
        setVerticalAlignment(JLabel.TOP);
        setHorizontalAlignment(JLabel.LEFT);

    }

    /**
     * Creates text rotated by angle in an anti-clockwise rotation
     * @param angle angle to be rotated by in degrees
     * @exception Exception if angleUnit is not RotatableText.DEGREES or RotatableText.RADIANS
     */
    public RotatableText(String text, double angle, final String angleUnit) throws IllegalAngleUnitException
    {
        if (!(angleUnit.equals(DEGREES) || angleUnit.equals(RADIANS)))
        {
            throw new IllegalAngleUnitException("Invalid Angle Selected");
        }
        else if (angleUnit.equals(DEGREES))
        {
            this.angle = angle * TO_RADIANS;
            super.setText(text);
        }
        else if (angleUnit.equals(RADIANS))
        {
            this.angle = angle;
            super.setText(text);
        }
        setVerticalAlignment(JLabel.BOTTOM);
        setHorizontalAlignment(JLabel.CENTER);
        }

    /**
     * Draws the Component
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.rotate(angle, getPreferredSize().width/2, getPreferredSize().height/2);
        g2.drawString(super.getText(), 0, 0);
        setBounds(getX(), getY());
        super.paintComponent(g);
    }

    /**
     * Gets the text of this RotatableText.
     * @return text
     */
    public String getText()
    {
        return super.getText();
    }

    /**
     * Set's bounds with a fixed size
     */
    public void setBounds(int x, int y)
    {
        super.setBounds(x, y, 100, 100);
    }

    public void setText(String text)
    {
        super.setText(text);
        repaint();
    }

    /**
     * Set the angle of this RotatableText in Radians
     * @param angle
     */
    public void setAngle(double angle)
    {
        this.angle = angle;
        repaint();
    }

    /**
     * Sets the angle of this RotatableText in the specified unit
     * @param angle
     * @param angleUnit
     * @throws IllegalAngleUnitException
     */
    public void setAngle(double angle, String angleUnit) throws     IllegalAngleUnitException
    {
        if (!(angleUnit.equals(DEGREES) || angleUnit.equals(RADIANS)))
        {
            throw new IllegalAngleUnitException("Invalid Angle Selected");
        }
        else if (angleUnit.equals(DEGREES))
        {
            this.angle = angle * TO_RADIANS;
        }
        else if (angleUnit.equals(RADIANS))
        {
            this.angle = angle;
        }
        repaint();
    }

    /**
     * Gets the angle of this RotatableText, anti-clockwise from the horizontal, in degrees.
     * @return
     */
    public double getAngle()
    {
        return angle * TO_DEGREES;
    }
}

每个Label生成如下(这是长度标签)

lengthLabel = new RotatableText("0", 14, true, RotatableText.DEGREES);  

每个标签都通过从其各自的文本字段中获取文本并将其作为参数传递给 label.setText().

来更新

编辑:打印 System.out.println(heightLabel.getText()) 只打印一份文本。

如果有人知道为什么会出现这种重复,我很想听听他们的意见。
谢谢,
山姆

您在代码中绘制了两次文本,如下所示:

@Override
protected void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    g2.rotate(angle, getPreferredSize().width/2, getPreferredSize().height/2);
    g2.drawString(super.getText(), 0, 0);  // ****** here *****
    setBounds(getX(), getY());
    super.paintComponent(g);             // ******* here ******
}

此外,我不会在绘画方法中设置组件的边界,这样做很危险,但请考虑在那里设置 Graphics2D 剪辑。此外,我会在 Graphics 对象的 copy 上进行旋转,然后在完成后处理副本。否则,您可能会在绘画链的下游传播旋转副作用(如果不需要)

例如,

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.rotate(angle, getPreferredSize().width / 2, getPreferredSize().height / 2);
    // g2.drawString(super.getText(), 0, 0);
    // setBounds(getX(), getY());

    // ***** This is a kludge and needs to be calculated better ****
    Rectangle clipBounds = g2.getClipBounds();
    int extraBounds = 10;
    int x = clipBounds.x - extraBounds;
    int y = clipBounds.y - extraBounds;
    int width = clipBounds.width + 2 * extraBounds;
    int height = clipBounds.height + 2 * extraBounds;
    g2.setClip(x, y, width, height);
    // ***** end kludge

    super.paintComponent(g2);
    g2.dispose();
}