当我试图从 Panel1 类的 Next JButton 触发动作事件时调用 Panel2 类的 JPanel panel2,我得到 NullPointerException。如何解决这个问题?请帮忙。
public class PanelEventTest
{
/**
* @param args
*/
JFrame frame;
void originalFrame()
{
frame = new JFrame();
frame.setSize(500, 300);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.add(new TestPanel1().panel1());
frame.add(new TestPanel2().panel2());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new PanelEventTest().originalFrame();
}
}
public class TestPanel1
{
JPanel panel1;
JButton next;
JPanel panel1()
{
panel1 = new JPanel();
next = new JButton("Next");
next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
new TestPanel2().panel2.removeAll();
}
});
panel1.add(next);
return panel1;
}
}
public class TestPanel2
{
JPanel panel2;
JList jlist;
String[] list = {"Sachin","Tarun","Vipin"};
JPanel panel2()
{
panel2 = new JPanel();
jlist = new JList(list);
panel2.add(jlist);
panel2.add(new JLabel("Test"));
return panel2;
}
}
我最后一个关于JPanel 的 Nullpointerexception问题已被你们成功解决。请帮忙。这个例外正在吃我的头。