java – 无法重新添加相同的面板
这让我疯了.当你点击3个不同的按钮时,我正在尝试更改面板,但它适用于一个面板 – 只有一次.
如果单击addPerson – “人员”面板显示, 在一个测试文件中,我创建了一个带有添加和删除的GUI:如果我点击添加它会抛出空指针异常,但如果我刚添加它并编译,那很好…… /* PERSON PANEL */ public JPanel create_PersonPnl() { personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); personPnl.setBackground(Color.WHITE); personPnl.setPreferredSize(minPnl); /* VERTICAL BOX for Person boxes */ Box personBox = Box.createVerticalBox(); personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY),"Person")); /* Horizontal Box for Name Lbl & TF */ Box nameBox = Box.createHorizontalBox(); nameBox.add(Box.createHorizontalStrut(10)); nameLbl = new JLabel("Name: "); nameBox.add(nameLbl); nameBox.add(Box.createHorizontalStrut(5)); nameTF = new JTextField(); nameBox.add(nameTF); nameBox.add(Box.createHorizontalStrut(10)); personBox.add(nameBox); personBox.add(Box.createVerticalStrut(10)); Box limitBox = Box.createHorizontalBox(); limitBox.add(Box.createHorizontalStrut(10)); limitLbl = new JLabel("CD Limit: "); limitBox.add(limitLbl); limitTF = new JFormattedTextField(); limitBox.add(limitTF); limitBox.add(Box.createHorizontalStrut(10)); personBox.add(limitBox); personBox.add(Box.createVerticalStrut(10)); personBox.add(addPersonBtn = new JButton("Add Person")); personBox.add(Box.createVerticalStrut(10)); personList = new JList(temp); personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrollp = new JScrollPane(personList); scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scrollp.setSize(100,45); personBox.add(scrollp); personBox.add(Box.createVerticalStrut(10)); Box optionsBox = Box.createHorizontalBox(); editPrsnBtn = new JButton("Edit"); editPrsnBtn.addActionListener(this); optionsBox.add(editPrsnBtn); removePrsnBtn = new JButton("Remove"); removePrsnBtn.addActionListener(this); optionsBox.add(removePrsnBtn); personBox.add(optionsBox); personPnl.add(personBox); return personPnl; } 这就是我的ActionPerformed方法中的内容. if(e.getSource() == addPersonBtn) { changePnl.removeAll(); changePnl.add(create_PersonPnl()); changePnl.revalidate(); System.out.println("PersonPnl added"); } if(e.getSource() == addCDBtn) { changePnl.removeAll(); changePnl.add(create_CDPnl()); changePnl.revalidate(); } if(e.getSource() == viewStoreBtn) { changePnl.removeAll(); changePnl.add(create_StorePnl()); changePnl.revalidate(); } 只有ChangePnl的代码. changePnl = new JPanel(); changePnl.setBackground(Color.WHITE); defaultPnl = new JPanel(); defaultPnl.setBackground(Color.WHITE); defaultPnl.add(new JLabel("Welcome to the CD Store")); defaultPnl.add(new JLabel("Click an option from the left")); changePnl.add(defaultPnl); S.O.P是调试以查看正在运行的内容..并且只打印一次,除非我取出.add(create_PersonPnl());所以我缩小了它,但我没有线索,因为它第一次工作. 预先感谢! 单独的测试文件来证明它是create_PersonPnl() import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder; public class Test implements ActionListener { JButton add,remove; JButton addPersonBtn,editPrsnBtn,removePrsnBtn; JFrame frame; JFormattedTextField limitTF; JLabel nameLbl,limitLbl; JList personList; JPanel TotalGUI,welcomePnl,mainPnl,imagePnl,changePnl,defaultPnl,cdPnl,storePnl; JPanel personPnl; JScrollPane scrollp; JTextField nameTF; Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); Dimension minPnl = new Dimension(300,400); /* Test for JList */ String[] temp = {"1","2","3","4","1","4" }; public Test() { frame = new JFrame("CD Store"); frame.setExtendedState(JFrame.NORMAL); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); //frame.pack(); //sets size based on components size in TotalGUI //frame.setJMenuBar(create_MenuBar()); //frame.setMinimumSize(minDim); frame.setSize(725,550); //set frame location (central to screen) int fw = frame.getSize().width; int fh = frame.getSize().height; int fx = (dim.width-fw)/2; int fy = (dim.height-fh)/2; frame.getContentPane().add(create_Content_Pane()); frame.setVisible(true); //moves the frame to the centre frame.setLocation(fx,fy); } public JPanel create_Content_Pane() { JPanel TotalGUI = new JPanel(); TotalGUI.add(remove = new JButton("Remove")); remove.addActionListener(this); TotalGUI.add(add = new JButton("Add")); <- this crashes when selected add.addActionListener(this); //TotalGUI.add(create_PersonPnl()); <- works just fine return TotalGUI; } public JPanel create_PersonPnl() { personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); personPnl.setBackground(Color.WHITE); personPnl.setPreferredSize(minPnl); /* VERTICAL BOX for Person boxes */ Box personBox = Box.createVerticalBox(); personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY),45); personBox.add(scrollp); personBox.add(Box.createVerticalStrut(10)); Box optionsBox = Box.createHorizontalBox(); editPrsnBtn = new JButton("Edit"); editPrsnBtn.addActionListener(this); optionsBox.add(editPrsnBtn); removePrsnBtn = new JButton("Remove"); removePrsnBtn.addActionListener(this); optionsBox.add(removePrsnBtn); personBox.add(optionsBox); personPnl.add(personBox); return personPnl; } public static void main(String[] args) { new Test(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == remove) { TotalGUI.removeAll(); TotalGUI.revalidate(); } if(e.getSource() == add) { TotalGUI.add(create_PersonPnl()); TotalGUI.revalidate(); } } } 解决方法
在你的方法
public JPanel create_Content_Pane() { JPanel TotalGUI = new JPanel(); TotalGUI.add(remove = new JButton("Remove")); remove.addActionListener(this); TotalGUI.add(add = new JButton("Add")); <- this crashes when selected add.addActionListener(this); //TotalGUI.add(create_PersonPnl()); <- works just fine return TotalGUI; } 发生NullPointerException是因为您的私有字段TotalGUI为null …在TotalGUI = new JPanel()之前删除JPanel声明;这将解决空指针问题.这可能只解决了测试场景中的问题…要解决原始场景中的问题,最好拥有该类的完整源代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java – 如何使用Apache Camel实现传递JAX-RS代理
- java – 如何防止Maven检查我未在settings.xml文
- java – JDBC MySql连接池实践,以避免用尽连接池
- java – 代码中的SwitchPreference和CheckBoxPre
- java使用归并删除法删除二叉树中节点的方法
- 无法使用Javamail通过SSL或TLS使用SMTP发送邮件
- Android系统下检测Wifi连接互联网是否正常的代码
- java – Spring Singleton Scope是如何收集垃圾的
- java – 我们是否可以在servlet-filter的url-map
- java – Web服务:JAX-WS,CXF,WSDL ……那是什么