文字被复制在JLabel

问题描述 投票:0回答:1

我设计的应用程序用于配置托盘,并且我已经得到了扩展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())打印只是一个文本的副本。

如果任何人有一个想法,为什么这种重复的现象发生,我很乐意听到他们的声音。 谢谢, 山姆。

java swing user-interface jlabel
1个回答
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夹在那里。我还要做图形对象的副本我的旋转,然后在完成时处置副本。否则你可能在画链下游传播旋转的副作用(如果不希望)

EG,

@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();
}
© www.soinside.com 2019 - 2024. All rights reserved.