如何在Grouplayout中使用多个JPanel

时间:2013-11-21 18:20:26

标签: java swing layout nullpointerexception grouplayout

我正在尝试向我的框架中添加多个JPanel,并让它们各自使用GroupLayout但由于某种原因,我在Thread main中获得NullPointerExceptionException。我可能做错了什么,我该怎么做呢?

然后我会glayout2 = new GroupLayout(temp2Panel); temp2Panel.setLayout(glayout2); glayout2.setAutoCreateGaps(true); glayout2.setAutoCreateContainerGaps(true);制作第二个有grplayout的面板并且会出现在同一个框架中吗?

    public class test extends JFrame {
   /**
    * 
    */
   private JLabel testing;

   private JLabel lblcode;
   private JLabel lblemployeedetails;
   private JLabel lblname;
   private JLabel lbladdress;
   private JLabel lblcity;
   private JLabel lblemail;
   private JLabel lbltelnumber;
   private JLabel lblcellnumber;

   private JLabel lblprnumber;
   private JLabel lblppe;
   private JLabel lblppedate;
   private JTextField txtprnumber;
   private JTextField txtppe;

   private JTextField txtcode;
   private JTextField txtname;
   private JTextField txtaddress;
   private JTextField txtaddress2;
   private JTextField txtcity;
   private JTextField txtemail;
   private JTextField txttelnumber;
   private JTextField txtcellnumber;

   private Container container1;
   private Container container2;

   private static final long serialVersionUID = 6666;

   private JPanel mainPanel = new JPanel();
   private JPanel temp1Panel = new JPanel();
   private JPanel temp2Panel = new JPanel();

   private GroupLayout glayout;

   public test() {
      // Frame Parameters
      setTitle("test");
      setSize(500,500);// Adjust size of the frame/window
      setLocationRelativeTo(null);// Adjust open location



      addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
/*************************************************************
 *  Properties~Always initialize within the constructor FOOL
*************************************************************/     
      testing = new JLabel("testing");

      lblcode= new JLabel("Code:");
      lblemployeedetails= new JLabel("Employee Details:");
      lblname= new JLabel("Name:");
      lbladdress= new JLabel("Address:");
      lblcity= new JLabel("City:");
      lblemail= new JLabel("Email:");
      lbltelnumber= new JLabel("Tel:");
      lblcellnumber= new JLabel("Cell:");
      lblprnumber = new JLabel("PR#:");
      lblppe = new JLabel("PPE:");
      lblppedate = new JLabel("PPE Date:");

      txtprnumber= new JTextField();
      txtppe= new JTextField();
      txtcode= new JTextField();
      txtname= new JTextField();
      txtaddress= new JTextField();
      txtaddress2= new JTextField();
      txtcity= new JTextField();
      txtemail= new JTextField();
      txttelnumber= new JTextField();
      txtcellnumber= new JTextField();
      //Below adding the 2 other panels to the main outer panel
      temp1Panel = new JPanel();
         temp1Panel.add(container1);
      temp2Panel = new JPanel();
      mainPanel = new JPanel();
         mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
         mainPanel.add(temp1Panel);
         mainPanel.add(temp2Panel);

/*************************************************************
 *               GroupLayout for temp1Panel
*************************************************************/

      container1 = getContentPane();
      glayout = new GroupLayout(temp1Panel);
      temp1Panel.setLayout(glayout);
      glayout.setAutoCreateGaps(true);
      glayout.setAutoCreateContainerGaps(true);


      glayout.setHorizontalGroup(
         glayout.createSequentialGroup()
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(lblprnumber)
               .addComponent(lblcode)
               .addComponent(lblname)
               .addComponent(lbladdress)
               .addComponent(lblcity)
               .addComponent(lblemail)
               .addComponent(lbltelnumber)
               .addComponent(lblcellnumber))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(txtprnumber)
               .addComponent(txtcode)
               .addComponent(txtname)
               .addComponent(txtaddress)
               .addComponent(txtaddress2)
               .addComponent(txtcity)
               .addComponent(txtemail)
               .addComponent(txttelnumber)
               .addComponent(txtcellnumber))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(lblppe))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(txtppe)
               .addComponent(lblemployeedetails))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(lblppedate))
      );

      glayout.setVerticalGroup(
         glayout.createSequentialGroup()
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblprnumber)
               .addComponent(txtprnumber)
               .addComponent(lblppe)
               .addComponent(txtppe)
               .addComponent(lblppedate))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblcode)
               .addComponent(txtcode)
               .addComponent(lblemployeedetails))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblname)
               .addComponent(txtname))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lbladdress)
               .addComponent(txtaddress))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(txtaddress2))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblcity)
               .addComponent(txtcity))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblemail)
               .addComponent(txtemail))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lbltelnumber)
               .addComponent(txttelnumber))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblcellnumber)
               .addComponent(txtcellnumber))


      );

      this.setVisible(true);

   }//test

/*************************************************************
 *    Main 
*************************************************************/

   public static void main(String[] args) {
          new test();

      }//main

}

编辑3:这就是我得到的:

Exception in thread "main" java.lang.IllegalArgumentException: GroupLayout can only be used with one Container at a time
    at javax.swing.GroupLayout.checkParent(Unknown Source)
    at javax.swing.GroupLayout.invalidateLayout(Unknown Source)
    at java.awt.Container.invalidate(Unknown Source)
    at java.awt.Component.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at javax.swing.JRootPane.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at java.awt.Window.addNotify(Unknown Source)
    at java.awt.Frame.addNotify(Unknown Source)
    at java.awt.Window.show(Unknown Source)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)

编辑4:

public class test extends JFrame {
   private static final int COLS = 30;
   /**
    * 
    */
   private JLabel testing;

   private JLabel lblcode;
   private JLabel lblemployeedetails;
   private JLabel lblname;
   private JLabel lbladdress;
   private JLabel lblcity;
   private JLabel lblemail;
   private JLabel lbltelnumber;
   private JLabel lblcellnumber;

   private JLabel lblprnumber;
   private JLabel lblppe;
   private JLabel lblppedate;
   private JTextField txtprnumber;
   private JTextField txtppe;

   private JTextField txtcode;
   private JTextField txtname;
   private JTextField txtaddress;
   private JTextField txtaddress2;
   private JTextField txtcity;
   private JTextField txtemail;
   private JTextField txttelnumber;
   private JTextField txtcellnumber;

   private static final long serialVersionUID = 6666;

   private JPanel mainPanel = new JPanel();
   private JPanel temp1Panel = new JPanel();
   private JPanel temp2Panel = new JPanel();

   private GroupLayout glayout;
   private GroupLayout glayout2;

   public test() {
      // Frame Parameters
      setTitle("test");
      setSize(500,500);// Adjust size of the frame/window
      setLocationRelativeTo(null);// Adjust open location



      addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
/*************************************************************
 *  Properties~Always initialize within the constructor FOOL
*************************************************************/
      testing = new JLabel("test");

      lblcode = new JLabel("Code:");
      lblemployeedetails = new JLabel("Employee Details:");
      lblname = new JLabel("Name:");
      lbladdress = new JLabel("Address:");
      lblcity = new JLabel("City:");
      lblemail = new JLabel("Email:");
      lbltelnumber = new JLabel("Tel:");
      lblcellnumber = new JLabel("Cell:");
      lblprnumber = new JLabel("PR#:");
      lblppe = new JLabel("PPE:");
      lblppedate = new JLabel("PPE Date:");

      txtprnumber = new JTextField(COLS);
      txtppe = new JTextField(COLS);
      txtcode = new JTextField(COLS);
      txtname = new JTextField(COLS);
      txtaddress = new JTextField(COLS);
      txtaddress2 = new JTextField(COLS);
      txtcity = new JTextField(COLS);
      txtemail = new JTextField(COLS);
      txttelnumber = new JTextField(COLS);
      txtcellnumber = new JTextField(COLS);
      //Below adding the 2 other panels to the main outer panel
      temp1Panel = new JPanel();
      temp2Panel = new JPanel();
      mainPanel = new JPanel();
         mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
         mainPanel.add(temp1Panel);
         mainPanel.add(temp2Panel);
/*************************************************************
 *               GroupLayout for temp1Panel
*************************************************************/

      getContentPane().add(temp1Panel);//mainPanel

      glayout = new GroupLayout(temp1Panel);
      temp1Panel.setLayout(glayout);
      glayout.setAutoCreateGaps(true);
      glayout.setAutoCreateContainerGaps(true);   

      glayout.setHorizontalGroup(
         glayout.createSequentialGroup()
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(lblprnumber)
               .addComponent(lblcode)
               .addComponent(lblname)
               .addComponent(lbladdress)
               .addComponent(lblcity)
               .addComponent(lblemail)
               .addComponent(lbltelnumber)
               .addComponent(lblcellnumber))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(txtprnumber)
               .addComponent(txtcode)
               .addComponent(txtname)
               .addComponent(txtaddress)
               .addComponent(txtaddress2)
               .addComponent(txtcity)
               .addComponent(txtemail)
               .addComponent(txttelnumber)
               .addComponent(txtcellnumber))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(lblppe))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(txtppe)
               .addComponent(lblemployeedetails))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.LEADING)
               .addComponent(lblppedate))
      );

      glayout.setVerticalGroup(
         glayout.createSequentialGroup()
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblprnumber)
               .addComponent(txtprnumber)
               .addComponent(lblppe)
               .addComponent(txtppe)
               .addComponent(lblppedate))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblcode)
               .addComponent(txtcode)
               .addComponent(lblemployeedetails))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblname)
               .addComponent(txtname))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lbladdress)
               .addComponent(txtaddress))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(txtaddress2))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblcity)
               .addComponent(txtcity))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblemail)
               .addComponent(txtemail))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lbltelnumber)
               .addComponent(txttelnumber))
            .addGroup(glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
               .addComponent(lblcellnumber)
               .addComponent(txtcellnumber))

      );

      getContentPane().add(temp2Panel);

      glayout2 = new GroupLayout(temp2Panel);
      temp1Panel.setLayout(glayout2);
      glayout2.setAutoCreateGaps(true);
      glayout2.setAutoCreateContainerGaps(true);  

      glayout2.setHorizontalGroup(
            glayout2.createSequentialGroup()
               .addGroup(glayout2.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(testing))

      );

      glayout2.setVerticalGroup(
            glayout2.createSequentialGroup()
               .addGroup(glayout2.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(testing))

      );






      this.setVisible(true);

   }//test

/*************************************************************
 *    Main 
*************************************************************/

   public static void main(String[] args) {
          new test();

      }//main

}

1 个答案:

答案 0 :(得分:3)

抛出NPE的行将告诉你究竟出了什么问题:

  temp1Panel.add(container1);

在初始化之前,您尝试使用组件container1。

建议:

  • 不要这样做。在使用之前先初始化变量。
  • 不要刷掉异常,而是批判性地阅读它们并研究抛出它们的行,因为这样做会为你节省很多时间。
  • 如果您将来需要有关例外的帮助,请务必发布异常堆栈跟踪,并在发布的代码中显示明显的评论// **** NullPointerException here ****
  • 你的代码有点奇怪。您似乎正在将contentPane添加到您的某个组件中 - 它应该是相反的方式。编译后编写的代码将显示组件。
  • 我同意Sage,GroupLayout很难用于手动编码,但话虽如此,这并非不可能,而且之前我已经使用过它。

修改

  • 你甚至不需要container1变量,因为你什么都不做。从本质上讲,它会让你感到烦恼并为你提供无价值,所以要摆脱它。

您的代码经过了一些修改:

import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {
   private static final long serialVersionUID = 6666;
   private static final int COLS = 10;

   private JLabel lblcode;
   private JLabel lblemployeedetails;
   private JLabel lblname;
   private JLabel lbladdress;
   private JLabel lblcity;
   private JLabel lblemail;
   private JLabel lbltelnumber;
   private JLabel lblcellnumber;

   private JLabel lblprnumber;
   private JLabel lblppe;
   private JLabel lblppedate;
   private JTextField txtprnumber;
   private JTextField txtppe;

   private JTextField txtcode;
   private JTextField txtname;
   private JTextField txtaddress;
   private JTextField txtaddress2;
   private JTextField txtcity;
   private JTextField txtemail;
   private JTextField txttelnumber;
   private JTextField txtcellnumber;

   private JPanel mainPanel = new JPanel();
   private JPanel temp1Panel = new JPanel();
   private JPanel temp2Panel = new JPanel();

   private GroupLayout glayout;

   public Test() {
      // Frame Parameters
      setTitle("test");
      // !! setSize(500, 500);// Adjust size of the frame/window
      // !! setLocationRelativeTo(null);// Adjust open location

      addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      // !! testing = new JLabel("testing");

      lblcode = new JLabel("Code:");
      lblemployeedetails = new JLabel("Employee Details:");
      lblname = new JLabel("Name:");
      lbladdress = new JLabel("Address:");
      lblcity = new JLabel("City:");
      lblemail = new JLabel("Email:");
      lbltelnumber = new JLabel("Tel:");
      lblcellnumber = new JLabel("Cell:");
      lblprnumber = new JLabel("PR#:");
      lblppe = new JLabel("PPE:");
      lblppedate = new JLabel("PPE Date:");

      // !! add COLS column size field
      txtprnumber = new JTextField(COLS);
      txtppe = new JTextField(COLS);
      txtcode = new JTextField(COLS);
      txtname = new JTextField(COLS);
      txtaddress = new JTextField(COLS);
      txtaddress2 = new JTextField(COLS);
      txtcity = new JTextField(COLS);
      txtemail = new JTextField(COLS);
      txttelnumber = new JTextField(COLS);
      txtcellnumber = new JTextField(COLS);
      temp1Panel = new JPanel();

      // add before using:
      // !! container1 = getContentPane(); // !!
      // !! temp1Panel.add(container1);
      temp2Panel = new JPanel();
      mainPanel = new JPanel();
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
      mainPanel.add(temp1Panel);
      mainPanel.add(temp2Panel);

      getContentPane().add(mainPanel); // !!

      glayout = new GroupLayout(temp1Panel);
      temp1Panel.setLayout(glayout);
      glayout.setAutoCreateGaps(true);
      glayout.setAutoCreateContainerGaps(true);

      glayout
            .setHorizontalGroup(glayout
                  .createSequentialGroup()
                  .addGroup(
                        glayout
                              .createParallelGroup(
                                    GroupLayout.Alignment.LEADING)
                              .addComponent(lblprnumber).addComponent(lblcode)
                              .addComponent(lblname).addComponent(lbladdress)
                              .addComponent(lblcity).addComponent(lblemail)
                              .addComponent(lbltelnumber)
                              .addComponent(lblcellnumber))
                  .addGroup(
                        glayout
                              .createParallelGroup(
                                    GroupLayout.Alignment.LEADING)
                              .addComponent(txtprnumber).addComponent(txtcode)
                              .addComponent(txtname).addComponent(txtaddress)
                              .addComponent(txtaddress2).addComponent(txtcity)
                              .addComponent(txtemail)
                              .addComponent(txttelnumber)
                              .addComponent(txtcellnumber))
                  .addGroup(
                        glayout.createParallelGroup(
                              GroupLayout.Alignment.LEADING).addComponent(
                              lblppe))
                  .addGroup(
                        glayout
                              .createParallelGroup(
                                    GroupLayout.Alignment.LEADING)
                              .addComponent(txtppe)
                              .addComponent(lblemployeedetails))
                  .addGroup(
                        glayout.createParallelGroup(
                              GroupLayout.Alignment.LEADING).addComponent(
                              lblppedate)));

      glayout.setVerticalGroup(glayout
            .createSequentialGroup()
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblprnumber).addComponent(txtprnumber)
                        .addComponent(lblppe).addComponent(txtppe)
                        .addComponent(lblppedate))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblcode).addComponent(txtcode)
                        .addComponent(lblemployeedetails))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblname).addComponent(txtname))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lbladdress).addComponent(txtaddress))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(txtaddress2))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblcity).addComponent(txtcity))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblemail).addComponent(txtemail))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lbltelnumber).addComponent(txttelnumber))
            .addGroup(
                  glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblcellnumber)
                        .addComponent(txtcellnumber))

      );

      pack(); // !!
      setLocationRelativeTo(null); // !!
      this.setVisible(true);

   }

   public static void main(String[] args) {
      new Test();

   }

}

编辑2 关于您对问题的修改,

  

然后我会做glayout2 = new GroupLayout(temp2Panel); temp2Panel.setLayout(glayout2); glayout2.setAutoCreateGaps(真); glayout2.setAutoCreateContainerGaps(真);使用grouplayout创建第二个面板并将出现在同一个Frame中?

当然,这可行。你尝试的时候会发生什么?


编辑3
异常堆栈跟踪表明您正在尝试将一个GroupLayout添加到多个容器中。但是您发布的代码并未反映此问题。请在最新版本的底部添加另一个编辑,其中一个代码会生成您在上面显示的异常。