2010-01-20 108 views
1

好了說,我有另一種的JPanel中的JPanel的「控制」和JPanel的「graphPanel」重繪兒童的JPanel

public class outer extends JPanel implements ActionListener{ 
    private JPanel controls,graphPanel; 
    private JButton doAction 

    public outer(){ 
     JPanel controls = new JPanel(); 
     JButton doAction = new JButton("Do stuff"); 
     doAction.addActionListener(this); 
     controls.add(doAction); 

     JPanel graphPanel = new JPanel(); 
     this.add(controls); 
     this.add(graphPanel); 
    } 



public void actionPerformed(ActionEvent e) { 
    if(e.getSource()==doAction){ 
     //How do I fire paintComponent of controls JPanel on this click 
} 

怎樣使「graphPanel」重繪我的按鈕被點擊

+0

看看重新驗證()的調用的JPanel。 – Kylar

+0

如果我打電話重繪兩次它的作品 – user252693

回答

1

的doAction後和graphPanel聲明兩次 - 一次在類的級別,然後再在方法:

private JPanel controls,graphPanel; // declared here first 
    private JButton doAction; // declared here first 

    public outer(){ 
     JPanel controls = new JPanel(); // Whoops, declared here again 
     JButton doAction = new JButton("Do stuff"); // Whoops, declared here again 
     doAction.addActionListener(this); 
     controls.add(doAction); 

     JPanel graphPanel = new JPanel(); // Whoops, declared here again 
     ... 

在該方法中刪除聲明,並讓他們簡單的任務,像這樣:

controls = new JPanel(); // no leading 'JPanel' 

做到這一點,額外的重繪代碼不會拋出NPE