0

我必须创建一个小程序。我写的代码如下。

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class feedback extends JApplet

    implements ActionListener
{
    private JTextField login;

    private JTextField email;
    private JTextField comments;
    private final String SUBMIT="SUBMIT";
    private final String CLEAR="CLEAR";
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(CLEAR.equals(command))
            {login.setText(" ");
            email.setText(" ");
            comments.setText(" ");}
        else if(SUBMIT.equals(command))
           {
            login.setText(" ");
            email.setText(" ");
            comments.setText(" ");
           }
        }
     public void start()
    {
        Container contentPane = getContentPane();
        JScrollPane sPane = new JScrollPane();
        JPanel pContPanel = new JPanel();

        pContPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints(3, 4, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,10,10,10), 20, 20);

        JLabel title = new JLabel("FEEDBACK");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        pContPanel.add(title, gbc);

        JPanel panel1 = new JPanel();
        JLabel prompt = new JLabel("LOGIN");
        panel1.add(prompt, gbc);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        pContPanel.add(panel1, gbc); 

        login = new JTextField(15);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        pContPanel.add(login, gbc);

        JPanel panel2=new JPanel();
        JLabel print = new JLabel("EMAIL");
        panel2.add(panel2);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        pContPanel.add(panel2, gbc);

        email = new JTextField(30);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        pContPanel.add(email, gbc);

        JPanel panel3=new JPanel();
        JLabel ask = new JLabel("COMMENTS");
        panel3.add(panel3);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        pContPanel.add(panel3, gbc);

        comments = new JTextField(50);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        pContPanel.add(comments, gbc);

        JButton bSUBMIT = new JButton(SUBMIT);
        bSUBMIT.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        pContPanel.add(bSUBMIT, gbc);

        JButton bCLEAR = new JButton(CLEAR);
        bCLEAR.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 4;
        pContPanel.add(bCLEAR, gbc);


         sPane.setViewportView(pContPanel);
         contentPane.add(sPane, BorderLayout.CENTER);
        }
    }

它正在编译,没有语法错误。但是,当我使用小程序查看器(在 BlueJ 中)运行它时,它会说,

异常:java.lang.IllegalArgumentException:将容器的父级添加到自身。

你能帮我找出我的代码中的错误在哪里,我可以通过什么方式纠正它?谢谢你。

4

1 回答 1

3

您正在尝试将面板添加到自身,例如panel2.add(panel2);

正确的程序应该是:

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class Feedback extends JApplet implements ActionListener
{
    private JTextField login;

    private JTextField email;
    private JTextField comments;
    private final String SUBMIT="SUBMIT";
    private final String CLEAR="CLEAR";
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(CLEAR.equals(command))
            {login.setText(" ");
            email.setText(" ");
            comments.setText(" ");}
        else if(SUBMIT.equals(command))
           {
            login.setText(" ");
            email.setText(" ");
            comments.setText(" ");
           }
        }
     public void start()
    {
        Container contentPane = getContentPane();
        JScrollPane sPane = new JScrollPane();
        JPanel pContPanel = new JPanel();

        pContPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints(3, 4, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,10,10,10), 20, 20);

        JLabel title = new JLabel("FEEDBACK");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        pContPanel.add(title, gbc);

        JPanel panel1 = new JPanel();
        JLabel prompt = new JLabel("LOGIN");
        panel1.add(prompt, gbc);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        pContPanel.add(panel1, gbc);

        login = new JTextField(15);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        pContPanel.add(login, gbc);

        JPanel panel2=new JPanel();
        JLabel print = new JLabel("EMAIL");
        panel2.add(print);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        pContPanel.add(panel2, gbc);

        email = new JTextField(30);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        pContPanel.add(email, gbc);

        JPanel panel3=new JPanel();
        JLabel ask = new JLabel("COMMENTS");
        panel3.add(ask);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        pContPanel.add(panel3, gbc);

        comments = new JTextField(50);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        pContPanel.add(comments, gbc);

        JButton bSUBMIT = new JButton(SUBMIT);
        bSUBMIT.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        pContPanel.add(bSUBMIT, gbc);

        JButton bCLEAR = new JButton(CLEAR);
        bCLEAR.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 4;
        pContPanel.add(bCLEAR, gbc);


         sPane.setViewportView(pContPanel);
         contentPane.add(sPane, BorderLayout.CENTER);
        }
    }
于 2012-09-19T07:28:13.823 回答