當我嘗試在actionPerformed方法內調用panel2時,我得到nullpointerexception。 Plz幫助我擺脫這種愚蠢的感覺。Nullpointerexception與JPanel
public class PanelEventTest implements ActionListener {
/**
* @param args
*/
JFrame frame;
JPanel panel1, panel2;
String[] list = {"Sachin","Tarun","Vipin"};
JList jlist;
JButton next;
void originalFrame()
{
frame = new JFrame();
frame.setSize(500, 300);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.add(panel1());
frame.add(panel2());
}
JPanel panel1()
{
panel1 = new JPanel();
next = new JButton("Next");
next.addActionListener(new PanelEventTest());
panel1.add(next);
return panel1;
}
JPanel panel2()
{
panel2 = new JPanel();
jlist = new JList(list);
panel2.add(jlist);
panel2.add(new JLabel("Test"));
return panel2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new PanelEventTest().originalFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "Next")
{
panel2.removeAll();
}
}
}
動作事件沒有附加panel2,是這個異常背後的原因?
歡迎計算器。好心地更新您的問題與錯誤stacktrace –