How to rotate Swing text?

邮差的信 提交于 2019-12-10 04:08:01

问题


Is there a way to rotate Swing text such as in a JLabel between 0 and 360 (or between -180 and 180) degrees in 1 degree steps?


回答1:


Yes. Look at 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). e.g.

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)




回答2:


I do not believe that Swing offers explicit support for this.
However, you can turn your text into an image, and rotate that, using the AffineTransform class.

Here is some example code, apparently taken from the book "Swing Hacks", for writing text backwards. You can easily modify it for rotating text, although you will have to add some code for the animation effect.




回答3:


Not JLabel but JEditorPane content http://java-sl.com/vertical.html



来源:https://stackoverflow.com/questions/8563037/how-to-rotate-swing-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!