我一直收到这个错误
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 customerorder)
customorder
是CustomerOrder
的实例,由JFrame
扩展而来;public class CustomerOrder extends JFrame
如错误状态illegalargumentexception adding a window to a container
;将一个窗口添加到另一个窗口是非法的,这根本没有意义。
这是我们不鼓励从像JFrame
这样的顶级容器直接扩展的原因之一。
让CustomerOrder
像任何正常框架一样可见(尽管这可能会导致其他问题),或者更好的是,将CustomerOrder
组件作为JPanel
的基础
public class CustomerOrder extends JPanel {
//...
}
这样你就可以把它添加到任何你需要的容器中。
我不知道这个逻辑是否正确,但只是玩玩,试图找出原因,它不适合我自己,我得出的结论是:
-
扩展
JFrame
导致只创建一个对象,为什么它试图将JFrame frame;
对象添加到自己,从而导致异常… -
扩展
JPanel
允许JFrame
帧;<——要划分为容器的对象&一个组件,它允许你frame.add(this)
. .
frame.add(this)
是由JPanel
方法.add()
调用的组件
如果这是错误的,请纠正…