Java 错误:IllegalArgumentException:将窗口添加到容器

Java Error: IllegalArgumentException: adding a window to a container(Java 错误:IllegalArgumentException:将窗口添加到容器)

本文介绍了Java 错误:IllegalArgumentException:将窗口添加到容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到错误:

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)

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

Can someone please help me and tell me what I'm doing wrong? I am beginner programmer.

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) {

     }

}

推荐答案

EmployeeGUI 继承自 JFrame,但是在你的 main 方法中,你正在创建一个新的 JFrame 并尝试向其中添加 EmployeeGUI 的实例.

EmployeeGUI extends from JFrame, but in your main method, you are creating a new JFrame and are trying to add an instance of EmployeeGUI to it.

更改 EmployeeGUI 使其从 JPanel 扩展而不是

Change EmployeeGUI so it extends from JPanel instead

这篇关于Java 错误:IllegalArgumentException:将窗口添加到容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java 错误:IllegalArgumentException:将窗口添加到容器

基础教程推荐