2016-05-30 66 views
0

上午試圖調用行動油漆執行,但它不能正常工作的Java如何調用paint在actionperform

btnNewButton_5.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      class MyCanvas extends JComponent { 

       public void paint(Graphics g) { 
        g.drawLine(10,20,50,60); 
       } 
      } 


      JFrame window = new JFrame(); 
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      window.setBounds(30, 30, 300, 300); 
      window.getContentPane().add(new MyCanvas()); 
      window.setVisible(true); 

     } 
    }); 

問題是,它僅顯示框,但矩形不畫

我怎麼可以編輯這個代碼幫助我

+1

http://stackoverflow.com/questions/21421150/java-using-graphics-component-within-an-actionlistener – pinkpanther

回答

0

首先,您應該在您的類上實現paint()方法,然後將它作爲簡單函數調用在ActionListener上使用repaint()方法。

+0

請舉例, –