JAVA 在Canvas上输入"你好"两个字..
Canvas 背景为黑色 输入的字体为黄色 
小弟刚学JAVA 可以写的详细些吗?

解决方案 »

  1.   

    setBackGroundColor(Color.black);public void paintComponent(Graphics g){
    super.paintComponent(g);Graphics2D g2 = (Graphics2D)g;
    g2.setColor(Color.yellow);
    g2.setFont(font);//字体
    g2.drawString("你好",x,y);//为起点坐标
    }
      

  2.   

    小弟新手 楼上的我做了 怎么没有触发
    public void paintComponent(Graphics g)事件呢?
    可以给写的详细些吗?
      

  3.   

    public class show  extends JFrame implements Runnable
    {
     static JFrame jf;
      Canvas cv;
      public show()
     {
      jf=new JFrame("时间显示");
      jf.setLayout(null);
       cv=new Canvas();
       cv.setBackground(Color.black);
       cv.setBounds(10,10,150,30);
      jf.add(cv);
      jf.setSize(200,100);  
       }
    public void paint(Graphics g) {
    System.out.println("aaaa");
       super.paint(g);
        Graphics2D g2 = (Graphics2D)g; 
         g2.setColor(Color.yellow); 
         g2.setFont(new Font("宋体",Font.PLAIN,20));//字体 
         g2.drawString("你好",0,0);//
      }
     public static void main(String arg[])
     {
     show t=new show();
       new Thread(t).start();
      jf.setVisible(true);  
     } 
    }
    运行程序没有触发System.out.println("aaaa");
      

  4.   

    晕死,忘记了Canvas是Component,paintComponent是JComponent的方法。public void paint(Graphics g){ 
    super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; 
    g2.setColor(Color.yellow); 
    g2.setFont(font);//字体 
    g2.drawString("你好",x,y);//为起点坐标 
    }