在JLabel中复制文本

时间:2019-02-07 13:42:44

标签: java swing user-interface jlabel

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

重复标签文本图像:
Duplicate Label Text Image

这是我的RotatableText类的源代码,它扩展了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;
    }
}

每个标签的生成方式如下(这是长度标签)

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

每个标签都是通过从其各自的文本字段获取文本并将其作为label.setText()的自变量进行传递来更新的。

编辑:打印System.out.println(heightLabel.getText())仅打印文本的一个副本。

如果有人对为什么会出现这种重复有一个想法,我很想听听他们的消息。
谢谢
山姆。

1 个答案:

答案 0 :(得分:1)

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

@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对象的副本进行旋转,然后在完成后处置副本。否则,冒着在涂漆链下游传播旋转副作用的风险(如果不需要的话)

例如

@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();
}