java.lang.illegalargumentexception将一个窗口添加到容器中

时间:2015-10-09 22:58:15

标签: java swing jframe illegalargumentexception

我一直收到此错误

    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.Container.*;

     public class main extends JFrame implements ActionListener
    {



    JButton CustomerOrder;
    JButton SoftwareProducts;
    JButton BooksProducts;
    JButton AddBooks;
    JButton StaffMember;
    JButton LoginView;


    static ArrayList<Software> softwareList = new ArrayList<Software>();

    CardLayout c1 = new CardLayout();


    JPanel mainPanel;
    SoftwareProducts addsoftware;

    CustomerOrder customerorder;

    BooksProducts addProduct;

    AddBooks addBook;

    SoftwareProducts sf = new SoftwareProducts();

    //Press submit;

    public static void main(String[] args)
   {

    new main();
    JFrame frame = new JFrame("Selling Product");
    frame.setLocationRelativeTo(null);
    //frame.getContentPane().add(main, BorderLayout.CENTER);
    //new CustomerOrder();
    //EmailFrame email=new EmailFrame();
    //CustomerOrder co = new CustomerOrder();
    //LoginView login = new LoginView();

  }

public main(){

    super();




  // getContentPane().setBackground(Color.green);





   setSize(700,800);
   setLayout(new FlowLayout());

     /*CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
     cardLayout.show(mainPanel, "softwareProducts");
     */


   //CustomerOrder co = new CustomerOrder();
   //add(co);



   SoftwareProducts = new JButton("SoftwareProducts");
   SoftwareProducts.setSize(150,40);
   SoftwareProducts.setLocation(20,50);


   CustomerOrder = new JButton("CustomerOrder");
   CustomerOrder.setSize(160,40);
   CustomerOrder.setLocation(150,50);



   BooksProducts = new JButton("BooksProducts");
   BooksProducts.setSize(200,40);
   BooksProducts.setLocation(310,50);


   AddBooks = new JButton("AddBook");
   AddBooks.setSize(200,40);
   AddBooks.setLocation(310,50);


   StaffMember = new JButton("StaffMember");
   StaffMember.setSize(200,40);
   StaffMember.setLocation(310,50);


   LoginView = new JButton("LoginView");
   LoginView.setSize(150,40);
   LoginView.setLocation(20,50);


   SoftwareProducts.addActionListener(this);

   add(SoftwareProducts);

   CustomerOrder.addActionListener(this);

   add(CustomerOrder);

   BooksProducts.addActionListener(this);

   add(BooksProducts);

   AddBooks.addActionListener(this);

   add(AddBooks);


   StaffMember.addActionListener(this);

   add(StaffMember);




   mainPanel = new JPanel();
   mainPanel.setBackground(Color.BLUE);
   mainPanel.setPreferredSize(new Dimension(600,500));
   mainPanel.setLayout(c1);







   addsoftware = new SoftwareProducts();
   mainPanel.add(addsoftware,"addsoftware");


   customerorder = new CustomerOrder();  ////// I am getting the error
        from this part of code  
   mainPanel.add(customerorder,"customerorder");///// I am getting the     
         error from this part of code  

   addProduct = new BooksProducts();
   mainPanel.add(addProduct,"addProduct");


   addBook = new AddBooks();
   mainPanel.add(addBook,"addBook");



  /*  mainPanel.add(SoftwareProducts);

     mainPanel = new JPanel(new CardLayout());

     mainPanel.add(SoftwareProducts);

     getContentPane().add(mainPanel);


     addProduct = new BooksProducts();
     mainPanel.add(addProduct,"addProduct");


    */




      add(mainPanel);






       setVisible(true);

}




public void actionPerformed(ActionEvent e)
{

   if(e.getSource() == SoftwareProducts)
   {

    c1.show(mainPanel,"addsoftware");
    this.setTitle("SoftwareProducts");
    //System.out.println("Softwares");
    }


    if(e.getSource() == CustomerOrder)
   {

    c1.show(mainPanel,"customerorder");
    this.setTitle("CustomerOrder");
    //System.out.println("Custmers");
    }


     if(e.getSource() == BooksProducts)
   {

     c1.show(mainPanel,"addProduct");
     this.setTitle("BooksProducts");
    }

    if(e.getSource() == AddBooks)
   {

     c1.show(mainPanel,"addBook");
     this.setTitle("AddBooks");
    }

   if(e.getSource() == LoginView)
   {

     c1.show(mainPanel,"LogIN");
     this.setTitle("LOGIN");
    }

}

      /*  public void actionPerformed(ActionEvent e)
        {
      if(e.getSource() == press)
 {

    System.out.println("addProducts");
    }

   }*/
  }

和我的customerOrder类

      import javax.swing.*;
      import javax.swing.table.*; 
      import java.awt.*;
      import java.awt.event.*;
      import java.util.Arrays; 
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;
      import java.util.*;
      import java.io.*;
       public class CustomerOrder extends JFrame 
     implements  ActionListener 

  {

//JButton asd = new JButton("BUTTON");
//JTextField productID;
//JTextField productName;
//JTextField productCost;
//JTextField productPYear;
//JTextField productPHouse;

JButton showOrder;


DefaultListModel<String> model = new DefaultListModel<>();
JList <String>orderList = new JList(model);
JTable softwareTabel = new JTable();


//DefaultTableModel tableModel = new DefaultTableModel(0, 1);


public CustomerOrder()
{

    //super();

   // JLabel toLabel=new JLabel("Product ID: ");
    //JTextField to=new JTextField();


    setLayout(new FlowLayout());


   /* 
    productName = new JTextField(12);
    add(productName);

    productCost = new JTextField(12);
    add(productCost);

    productPYear = new JTextField(12);
    add(productPYear);


    productPHouse = new JTextField(12);
    add(productPHouse);

    */
    showOrder = new JButton("SHOW ORDER");
    showOrder.setSize(25,40);
    showOrder.addActionListener(this);
    add(showOrder); 





}


public void actionPerformed(ActionEvent e)

{
    //Object[] colNames={"Product ID","Processor","RAm"};
    if(e.getSource() == showOrder)
    {  
         model.removeAllElements();

       /*  GridLayout exLayout = new GridLayout(3, 3);

         JLabel ram,processor; 

         ram = new JLabel("RAM");
         processor = new JLabel("Processor");
         String softwaredata[] = {"ID","RAM","Processor","Product ID","Product Name","Product Year","Product Year","Product PublishHouse"};

         JPanel panel = new JPanel();
         panel.setLayout(new GridLayout(8,   3));

         add(ram);
         add(processor);

         JTable table = new JTable();*/
         //DeafultTableModel dm = new DeafultTableModel(0,0);
          //String header[] = new String[] {"RAM", "Processor","ProductID","Product Name","Product Year","Product Publish House"}; 
          //dm.setColumnIdentifiers(header);
          //Object[][] software = new Object[8][3];

          //model.addRow(orderList.toArray());



          int x = 0;
       while (x < main.softwareList.size())
        {
            //./model.addElement(main.softwareList.get(x).getproductYear());
             model.addElement(""+main.softwareList.get(x).getproductID());
             model.addElement(""+main.softwareList.get(x).getRam());
             model.addElement(""+ main.softwareList.get(x).getProcessor());
             model.addElement(""+ main.softwareList.get(x).getproductID()); 
             model.addElement(main.softwareList.get(x).getproductName());
             model.addElement(""+ main.softwareList.get(x).getproductYear());
             model.addElement(main.softwareList.get(x).getproductPublishHouse());
             //model.addElement(main.softwareList.get(x).getproductID());*/              

             x++;

          //System.out.println("as");


        }
          /* ArrayList<String> table1 = new ArrayList();

          ArrayList<ArrayList<String>> table2 = new ArrayList();
          table1.add("Product ID");
          table1.add("RAM");
          table1.add("Processor");
          table1.add("Product Name");
          table1.add("Product Year");
          table1.add("Product PHouse");

          Object[] software = table1.toArray();
          String[] [] softwarest = new String[table1.size()][];

          int i = 0;
          for (List<String> next : table2){

             softwarest[i++] = next.toArray(new String[next.size()]);
            }



            JTable newTable = new JTable(softwarest,software);

            JFrame frame = new JFrame();
            frame.getContentPane().add(new JScrollPane(newTable));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            */
             //table.add(model);

         // add(orderList);
        //Object[] column = {"RAM:", "Processor:"};
        //Object[][] data = {{"RAM:", "Processor:"}};
        //JTable table = new JTable();
        //table.setShowGrid(false);
        //table.setTableHeader(null); 

    }



}



 }

当我运行此代码时,我从customerorder = new CustomerOrder中的主类中获得以下异常; mainPanel.add(customerorder,&#34; customerorder&#34)

2 个答案:

答案 0 :(得分:1)

customorderCustomerOrder的一个实例,其范围从JFrame开始; public class CustomerOrder extends JFrame

错误表明illegalargumentexception adding a window to a container;将窗口添加到另一个窗口是违法的,它根本没有意义。

这是我们不鼓励直接从JFrame等顶级容器扩展的原因之一。

CustomerOrder像任何正常的框架一样可见(尽管这可能会引导您进入其他问题)或者更好,基于CustomerOrder的{​​{1}}组件

JPanel

通过这种方式,您可以将其添加到您需要的容器中。

答案 1 :(得分:-1)

Idk,如果这种逻辑是正确的,但是在尝试找出它对我自己没有意义的原因时,我得出结论:

  • 扩展JFrame只会导致创建一个对象,并且为什么会尝试将JFrame frame;对象添加到自身而导致异常...

  • 扩展JPanel允许JFrame框架; <-对象分为一个容器和一个组件,使您可以 frame.add(this) ..

frame.add(this)JPanel方法.add()

调用的组件

如果这是错误的,请正确。