加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 无法重新添加相同的面板

发布时间:2020-12-15 02:31:34 所属栏目:Java 来源:网络整理
导读:这让我疯了.当你点击3个不同的按钮时,我正在尝试更改面板,但它适用于一个面板 – 只有一次. 如果单击addPerson – “人员”面板显示, 如果您然后单击addCD – CD面板显示; (视图商店相同) 如果您然后单击addPerson – 它不起作用.它抛出一个nullpointer异常.
这让我疯了.当你点击3个不同的按钮时,我正在尝试更改面板,但它适用于一个面板 – 只有一次.

如果单击addPerson – “人员”面板显示,
如果您然后单击addCD – CD面板显示; (视图商店相同)
如果您然后单击addPerson – 它不起作用.它抛出一个nullpointer异常.
即使您单击addCD / viewstore,然后添加它显示的Person,但它不会再次显示.

在一个测试文件中,我创建了一个带有添加和删除的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声明;这将解决空指针问题.这可能只解决了测试场景中的问题…要解决原始场景中的问题,最好拥有该类的完整源代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读