paint是自动调用的,不需要自己去调,把
   Graphics gg=new Graphics();//编译错误:类不能被初始化???
   paint(gg);
这两句删掉
而且初始化Graphics也不能用new,如下:
Graphics gg=new JFrame().getContentPane().getGraphics();

解决方案 »

  1.   

    paint是自动调用的,不需要自己去调,把
       Graphics gg=new Graphics();//编译错误:类不能被初始化???
       paint(gg);
    这两句删掉
    而且初始化Graphics也不能用new,如下:
    Graphics gg=new JFrame().getContentPane().getGraphics();
      

  2.   

    Graphics gg=getGraphics()//编译错误:类不能被初始化???
      

  3.   

    但我想按一个按钮在窗口中画一条线,却实现不了。能否指点一下?
    void jButton1_actionPerformed(ActionEvent e) {
        Graphics gg=new frmMain().getContentPane().getGraphics();
        //Color c= new Color(100,100,100);
        //gg.setColor(c) ;    
        gg.drawLine(0,0,10,10);
      }
      

  4.   

    void jButton1_actionPerformed(ActionEvent e) {
       repaint();
    }
    因为repaint()函数自动调用paintComponent(Graphics g),然后你把需要的画图代码填到重载的paintComponent里就行了
    paintComponent(Graphics g){
      g.drawLine(0,0,10,10);
    }
      

  5.   

    可是程序运行不会调用方法阿。。
      public void paintComponent(Graphics g){
        Color c= new Color(100,100,100);
        g.setColor(c) ;
        g.drawLine(0,0,10,10);
      }  void jButton1_actionPerformed(ActionEvent e) {
        repaint();
      }
      

  6.   

    应该这样写public void paint(Graphics g){
        paintComponent(g);
    }
    public void paintComponent(Graphics g){
        Color c= new Color(100,100,100);
        g.setColor(c) ;
        g.drawLine(0,0,100,100);
    }
    void jButton1_actionPerformed(ActionEvent e) {
        repaint();
    }