2012-09-19 2 views
0

애플릿을 만들어야합니다. 필자가 작성한 코드는 다음과 같습니다.Java 애플릿 오류 - IllegalArgumentException "컨테이너의 부모를 자체에 추가 중"

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 : 컨테이너의 부모를 자체에 추가하는 중입니다.

제 코드의 실수는 어디에서 알 수 있습니까? 어떻게 수정할 수 있습니까? 감사합니다.

+2

무엇 스택 트레이스 모습입니까? 그것은 무엇을 가리키는가? –

답변

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); 
     } 
    } 
+0

+1 매우 좋습니다. JLabel을 추가하는 대신 OP에서 CAP 오류가 발생했습니다. :-) – asgs

+0

고맙습니다. 문제를 해결했습니다. – Sarang

+0

대답을 수락하십시오. – Satya