嗨我正在创建一个Java桌面应用程序,我正在绘制矩形。我想在矩形内写一些文字。如何在矩形内写文本
我该如何做到这一点?
这里是我的代码:
class DrawPanel extends JPanel {
private void doDrawing(Graphics g) {
int a=90;
int b=60;
int c=10;
int d=15;
ArrayList<Graphics2D> g1 = new ArrayList<Graphics2D>();
for(int i=0;i<=9;i++){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(212, 212, 212));
g2d.drawRect(c, d, a, b);
d+=65;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
doDrawing关于Graphics2D的一切应该在paintComponent中,在doDrawing中只创建坐标,对象,在paintComponent里面只能在准备好的对象内部循环arrray – mKorbel
使用['drawString'](http://docs.oracle.com/ javase/7/docs/api/java/awt/Graphics.html#drawString(java.lang.String,int,int))方法。在'drawRect'之后。 – alex2410
还要考虑'TextLayout',见[这里](http://stackoverflow.com/a/4287269/230513)。 – trashgod