Java错误:IllegalArgumentException:向容器添加窗口

时间:2013-06-02 04:12:15

标签: java swing arguments containers

我一直收到错误:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
  at java.awt.Container.checkNotAWindow(Container.java:483)
  at java.awt.Container.addImpl(Container.java:1084)
  at java.awt.Container.add(Container.java:966)
  at Lab2.EmployeeGUI.main(EmployeeGUI.java:28)

有人可以帮助我并告诉我我做错了什么吗? 我是初学程序员。

package Lab2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Jim Doyle
 */
public class EmployeeGUI extends JFrame implements ActionListener {

    JTextField fName, mName, lName, phone, sal, years;
    JComboBox boxTitle, boxDept;
    DefaultListModel lstdefault;
    JList project;

    DbWork dbw = new DbWork("Lab2");
    DbWork Title = new DbWork("Lab2");
    DbWork Dept = new DbWork("Lab2");
    DbWork Prjs = new DbWork("Lab2");
    DbWork PrjList = new DbWork("Lab2");

    public static void main(String[] args) {
        EmployeeGUI app = new EmployeeGUI();
        JFrame frame = new JFrame("Employee Interface by Jim Doyle");
        frame.getContentPane().add(app, BorderLayout.CENTER);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public EmployeeGUI() {

        JPanel labels = new JPanel();
        labels.setLayout(new GridLayout(8,1));
        labels.add(new JLabel("First Name"));
        labels.add(new JLabel("MI"));
        labels.add(new JLabel("Last Name"));
        labels.add(new JLabel("Title"));
        labels.add(new JLabel("Telephone"));
        labels.add(new JLabel("Salary"));
        labels.add(new JLabel("Department"));
        labels.add(new JLabel("Years in Service"));

        getContentPane().add(labels, BorderLayout.WEST);


        JPanel fields = new JPanel();
        fields.setLayout(new GridLayout(8,1));
        fName = new JTextField(15);
        mName = new JTextField(15);
        lName = new JTextField(15);
        phone = new JTextField(15);
        sal = new JTextField(15);
        years = new JTextField(15);

        boxTitle = new JComboBox();
        boxDept = new JComboBox();

        fields.add(fName);
        fields.add(mName);
        fields.add(lName);
        fields.add(boxTitle);
        fields.add(phone);
        fields.add(sal);
        fields.add(years);

        getContentPane().add(fields, BorderLayout.CENTER);

        JPanel prjinfo = new JPanel();
        prjinfo.setLayout(new GridLayout(1,2));
        prjinfo.add(new JLabel("Project Description"));
        project = new JList();
        lstdefault = new DefaultListModel();

        // add items to title combo box
        while(Title.nextRecord()) {
            String txtTit = Title.getField(1);
            if(txtTit!=null) {
                boxTitle.addItem(Title.getField(1));
            }
        }

        // add items to department combo box
        while(Dept.nextRecord()) {
            String txtDept = Dept.getField(2);
            if(txtDept!=null) {
                boxDept.addItem(Dept.getField(2));
            }
        }

        while(PrjList.nextRecord()) {
            lstdefault.addElement(PrjList.getField(1));
        }

        project = new JList(lstdefault);
        project.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        prjinfo.add(project);
        getContentPane().add(prjinfo, BorderLayout.SOUTH);

    }

     public void actionPerformed(ActionEvent e) {
         String button = e.getActionCommand();

         if(button == "First") {
             if(dbw.firstRecord()) {
                Execute(); 
             }  
         }

         else if(button == "Next") {
             if(dbw.nextRecord()) {
                Execute(); 
             } 
         }

         else if(button == "Save") {
             String sql = "UPDATE FirstName, MiddleName, LastName, WorkPhone, Salary, YearsInService FROM Employee;";
             dbw.processQuery(sql);
         }

        }

     private void action() {
         boxTitle.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 JComboBox b = (JComboBox)e.getSource();
                 String ntitle = (String)b.getSelectedItem();
                 updateTitle(ntitle);
             }
         });
     }



     private void Execute() {
         fName.setText(dbw.getField(1));
         mName.setText(dbw.getField(2));
         lName.setText(dbw.getField(3));
         phone.setText(dbw.getField(5));
         sal.setText(dbw.getField(6));
         years.setText(dbw.getField(8));
         String ftext = dbw.getField(4);
         int dx = TitleList(ftext);
         boxTitle.setSelectedIndex(dx);
         String dtext = dbw.getField(7);
         int dx2 = DeptList(dtext);
         boxDept.setSelectedIndex(dx2);
         action();
     }

     int TitleList(String title) {
         int dx = 0;
         for(int z=0; z<boxTitle.getItemCount(); z++) {
             if(title.equals(boxTitle.getItemAt(z))) {
                 dx = z;
             }
         }
         return dx;
     }

     int DeptList(String dept) {
         int dx = 0;
         for(int z=0; z<boxDept.getItemCount(); z++) {
             if(dept.equals(boxDept.getItemAt(z))) {
                 dx = z;
             }
         }
         return dx;
     }

     private void updateTitle(String title) {

     }

}

3 个答案:

答案 0 :(得分:12)

EmployeeGUIJFrame延伸,但在您的main方法中,您正在创建新的JFrame,并且正在尝试添加EmployeeGUI的实例它

更改EmployeeGUI,使其从JPanel改为

答案 1 :(得分:6)

下面:

frame.getContentPane().add(app, BorderLayout.CENTER);

您正在尝试将JFrame添加到JFrame,这没有任何意义。

为什么不尝试显示应用而不是将其添加到JFrame。或者甚至更好,没有EmployeeGUI扩展JFrame。

答案 2 :(得分:2)

您无法将JFrame添加到另一个JFrame。 但您可以将JPanel添加到JFrame,换句话说,更改EmployeeGUI以使其扩展为JPanel。