简体   繁体   English

如何旋转Swing文本?

[英]How to rotate Swing text?

有没有办法旋转Swing文本,例如在JLabel中以0度到360度(或-180到180度)之间以1度的步长旋转?

Yes. 是。 Look at Graphics2D.rotate(). 看看Graphics2D.rotate()。 For a JLabel, I think you could override the paintComponent() method to call rotate(x), then call the existing paintComponent(), then call rotate(-x). 对于JLabel,我认为你可以覆盖paintComponent()方法来调用rotate(x),然后调用现有的paintComponent(),然后调用rotate(-x)。 eg 例如

protected void paintComponent(Graphics g) {
   Graphics2D g2 = ( Graphics2D )g;
   g2.rotate(theta);
   super.paintComponent(g2);
   g2.rotate(-theta);
}

I haven't tried this. 我没试过这个。 You might need to add an offset, see Graphics2D.rotate(double theta, double x, double y) 您可能需要添加偏移量,请参阅Graphics2D.rotate(double theta,double x,double y)

I do not believe that Swing offers explicit support for this. 我不相信Swing明确支持这一点。
However, you can turn your text into an image, and rotate that, using the AffineTransform class. 但是,您可以将文本转换为图像,然后使用AffineTransform类旋转它。

Here is some example code , apparently taken from the book "Swing Hacks", for writing text backwards. 下面是一些示例代码 ,显然是从“Swing Hacks”一书中提取的,用于向后编写文本。 You can easily modify it for rotating text, although you will have to add some code for the animation effect. 您可以轻松地修改它以旋转文本,但您必须为动画效果添加一些代码。

不是JLabel而是JEditorPane内容http://java-sl.com/vertical.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM