重绘儿童jpanel

时间:2010-01-20 15:09:58

标签: java swing 2d

好的说我在另一个jpanel中有一个JPanel“控件”和jpanel“graphPanel”

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”重绘

1 个答案:

答案 0 :(得分: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