NullPointerException继续

时间:2013-04-13 09:15:27

标签: java swing nullpointerexception jpanel

当我尝试从Panel1类的Next JButton触发动作事件时调用Panel2类的JPanel panel2时,我得到NullPointerException。怎么解决这个? plzz帮助。

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;
}
}

我们的上一个问题Nullpointerexception with JPanel已成功解决了。 Plz帮忙。这个例外正在吃我的脑袋。

1 个答案:

答案 0 :(得分:1)

如果您尝试更改此行:

new TestPanel2().panel2.removeAll();

要:

new TestPanel2().panel2().removeAll();

将解决问题,但目前的逻辑是有缺陷的。

更好的解决方案是:

TestPanel2更改为:

public class TestPanel2 {
    JPanel panel2;
    JList jlist;
    String[] list = { "Sachin", "Tarun", "Vipin" };

    public TestPanel2() { // was: JPanel panel2() {
        panel2 = new JPanel();
        jlist = new JList(list);
        panel2.add(jlist);
        panel2.add(new JLabel("Test"));
                      // was: return panel2;
    }
}

然后将TestPanel1修改为:

public class TestPanel1 {
    JPanel panel1;
    JButton next;

    public TestPanel1(final JFrame frame, TestPanel2 tp2) { // was: JPanel panel1() {
        panel1 = new JPanel();
        next = new JButton("Next");
        final JPanel panel2 = tp2.panel2; // line created
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                panel2.removeAll(); // was: new TestPanel2().panel2.removeAll();
                frame.validate(); // line created
                frame.paint(); // line created
            }
        });
        panel1.add(next);
                         // was: return panel1;
    }
}

最后,在PanelEventTest.originalFrame()上,更改:

frame.add(new TestPanel1().panel1());
frame.add(new TestPanel2().panel2());

为:

TestPanel2 tp2 = new TestPanel2();
frame.add(new TestPanel1(frame, tp2).panel1);
frame.add(tp2.panel2);
frame.validate();
frame.repaint();

说明

当您需要构造函数时,您正在创建方法。您必须读取此内容:Understanding constructors

此外,您需要将TestPanel2frame传递给TestPanel1

  • 您的代码正在创建一个新的TestPanel2(不附加到任何人),然后在其面板上调用removeAll()。这根本没有效果(因为这个面板没有显示在任何地方)。
  • 更改后的代码将在removeAll()的面板上调用TestPanel1
  • 此外,每次对组件进行更改时,都需要重新验证/重新显示组件。
    • 目前,您在创建frame(添加面板)时以及删除panel2时(在“下一步”按钮的操作中)更改了它们。