我有一个bigPanel。 bigPanel内部有多个面板(panel_1,panel_2,panel_3等......)
bigPanel使用GridBagLayout。 panel_1等使用GridLayout。
在另一个面板中,我创建了一个ADD按钮。如果用户单击此按钮,则会出现JDialog,用户必须填写表单:
用户填写完JDialog表单后,将根据JDialog表单创建一个新按钮。
此代码将在用户单击JDialog表单中的FINISHED按钮后执行:
if(Number=="1"){
if(Alphabet=="A")
{
JButton newButton = new JButton(buttonName);
//bigPanel.add(newButton);
//bigPanel.updateUI();
panel_1.add(newButton);
panel_1.updateUI();
JOptionPane.showMessageDialog(null,"Successfully added !");
}
}
else if(Alphabet=="B"){
JButton newButton = new JButton(buttonName);
//bigPanel.add(newButton);
//bigPanel.updateUI();
panel_2.add(newButton);
panel_2.updateUI();
JOptionPane.showMessageDialog(null,"Successfully added !");
}
...
}
if (Number=="2"){
...
}
...
我用数字1和字母A测试JDialog。目前我得到的结果是在控制台中警告该行有问题:
panel_1.add(newButton);//line 313
控制台中的警告:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at UI2$3.actionPerformed(UI2.java:313)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
未添加按钮。但是,如果我改为
bigPanel.add(newButton);
bigPanel.updateUI();
newButton被添加到bigPanel。不幸的是,我希望将newButton添加到panel_1而不是bigPanel ..
答案 0 :(得分:1)
请检查是否添加了panel_1