图片如上 代码在下面
package awt; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.*; import java.awt.geom.Rectangle2D; import java.util.Date; import javax.swing.*; public class DrawString extends JFrame { //三个对象 在图片上显示问题 private Shape rect; private Font font; private Date date; public DrawString() { rect=new Rectangle2D.Double(10,10,200,80); font=new Font("宋体",Font.BOLD,16); date=new Date(); //设置窗体大小 关闭模式 标题 面板等等 this.setSize(230,140); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new CanvasPanel()); this.setTitle("绘图文本"); } public static void main(String[]args) { new DrawString().setVisible(true); } class CanvasPanel extends Canvas{ public void paint(Graphics g) { super.paint(g); //绘制加填充 Graphics2D g2=(Graphics2D)g; g2.setColor(Color.green); g2.fill(rect); g2.setColor(Color.blue); g2.setFont(font); g2.drawString("现在时间是",20,30); g2.drawString(String.format("%tr",date), 50, 60); } } }