参考http://blog.china-pub.com/more.asp?name=jiangsukid&id=2276修改过来的。
import java.awt.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Panel1
extends JPanel { private double currentAngle; public Panel1() {
} public void rotate() {
//rotate 5 degrees at a time
currentAngle += 5.0;
if (currentAngle >= 360.0) {
currentAngle = 0;
}
repaint();
} protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) (origXform.clone());
//center of rotation is center of the panel
int xRot = this.getWidth() / 2;
int yRot = this.getHeight() / 2;
newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
g2d.setTransform(newXform);
g2d.drawString("测试数据", xRot, yRot);
} public static void main(String[] args) {
JFrame f = new JFrame();
Container cp = f.getContentPane();
cp.setLayout(new BorderLayout());
final MyPanel rotatePanel = new MyPanel();
JButton b = new JButton("Rotate");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
rotatePanel.rotate();
}
});
cp.add(rotatePanel, BorderLayout.CENTER);
cp.add(b, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}