8

我不断收到错误:

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

     }

}
4

3 回答 3

15

EmployeeGUIextends from JFrame,但在您的main方法中,您正在创建一个新的JFrame并尝试向EmployeeGUI其中添加一个实例。

改变所以它从而不是EmployeeGUI延伸JPanel

于 2013-06-02T04:14:32.587 回答
7

这里:

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

您正在尝试将 JFrame 添加到没有意义的 JFrame 中。

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

于 2013-06-02T04:14:31.103 回答
4

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

于 2018-05-24T03:22:34.050 回答