作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个大面板。 bigPanel 内部有多个面板(panel_1、panel_2、panel_3 等......)
bigPanel 使用 GridBagLayout。 panel_1等使用GridLayout。
在另一个面板中,我创建了一个“添加”按钮。如果用户单击此按钮,则会出现一个 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 中。
最佳答案
请检查是否添加了panel_1
关于java - 如何将按钮添加到特定的 JPanel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584485/
我是一名优秀的程序员,十分优秀!