无法在JFRAME组件中触发方法

时间:2016-12-09 16:59:55

标签: java swing

这是我遇到问题的地方: 当我想要ot火按钮Marchewkaim得到错误空指针异常:

public class MainFrame extends JFrame {
public static  void Draw()
{
    mp.repaint();
}
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
static private MyPanel mp = null;

/**
 * This method initializes mp   
 *  
 * @return MyPanel  
 */
private MyPanel getMp() {
    if (mp == null) {
        mp = new MyPanel();
        mp.setLayout(null);
    }
    return mp;
}

/**
 * @param args
 */
public static void main(String[] args) {

    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            MainFrame thisClass = new MainFrame();
            thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            thisClass.setVisible(true); 
        }
    });



}

/**
 * This is the default constructor
 */
public MainFrame() {
    super();
    initialize();
}

/**
 * This method initializes this
 * 
 * @return void
 */
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setLayout(new CardLayout(0, 0));

    }
    return jContentPane;
}
private void initialize() {

    this.setSize(668, 402);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
    JPanel panel = new JPanel();
    panel.setLayout(null);
    this.getContentPane().add(panel, "Main");
    panel.setVisible(true);
    JButton btnMarchewka = new JButton("Marchewka");
    btnMarchewka.setBounds(12, 25, 113, 49);
    panel.add(btnMarchewka);

    JButton btnArbuz = new JButton("Arbuz");
    btnArbuz.setBounds(12, 87, 113, 49);
    panel.add(btnArbuz);

    JButton btnUsun = new JButton("Usun ostatni");
    btnUsun.setBounds(12, 149, 113, 49);
    panel.add(btnUsun);
    btnMarchewka.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 
        {
            Draw();
        }
    });

}

/**
 * This method initializes jContentPane
 * 
 * @return javax.swing.JPanel
 */


}  //  @jve:decl-index=0:visual-constraint="10,10"

另一个班级

public class MyPanel extends JPanel {

public GObject c; 
public GObject w;
//  @jve:decl-index=0:
private static final long serialVersionUID = 1L;
public List<GObject> list= new ArrayList<GObject>();
/**
 * This is the default constructor
 */
public MyPanel() {
    super();
    initialize();
}
public void addCarrot()
{
    GObject[] c=new GObject[list.size()+1];
    c[list.size()]=new Carrot();
    list.add(c[list.size()]);
    MainFrame.Draw();
    }
public void addWatermelon()
{
    GObject[] w=new Watermelon[list.size()+1];
    w[list.size()]=new Watermelon();
    list.add(w[list.size()]);

    MainFrame.Draw();
    }
   public void remove()
   {
   list.remove(list.size()-1);

    MainFrame.Draw();
  }
  /**
 * This method initializes this
 * 
 * @return void
 */
private void initialize() {
    this.setSize(400, 400);
    this.setLayout(null);
}
@Override
protected void paintComponent(Graphics arg0) {
    // TODO Auto-generated method stub
    super.paintComponent(arg0);
    if
            (list!=null){
    for(int i=0;i<list.size();i++)
    {
        list.get(i).drawMe(arg0);
    }
    }
}

 }

我试图自己解决这个错误,但我无法找到我什么都没指的地方。这样做了6个小时,我生病了,累了。我正在寻找一些提示。

1 个答案:

答案 0 :(得分:0)

在MainFrame类中,你有getMp()方法来初始化mp,但是当你调用mp.repaint()时,它从不调用从Draw()方法引起NPE的方法。 MyPanel类中也有一些未使用的变量。