Java. Excepción en el hilo "principal" java.lang.IllegalArgumentException: agregar una ventana a un contenedor

I keep getting the following error when I try to run my program:

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 javax.swing.JTabbedPane.insertTab(JTabbedPane.java:724) at javax.swing.JTabbedPane.addTab(JTabbedPane.java:767) at CourserWindow.(CourserWindow.java:40) at CourserWindow.main(CourserWindow.java:58)

Aquí están mis archivos:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;

public class CourserWindow extends JFrame
{
    private JFrame panel;

    public CourserWindow()
    {
  //Set default window size
  final int WINDOW_WIDTH = 500;
  final int WINDOW_HEIGHT = 400;

  setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

  //Set title of window
  setTitle("Courser");

  //set close button functionality      
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //Add grid layout for left/right areas
  setLayout(new GridLayout(1, 2));

  //create left side
  JTabbedPane tabbedPane = new JTabbedPane();

  //create icons for tabs
  ImageIcon browserIcon = new ImageIcon("Icons/BrowserIcon.jpg");
  ImageIcon addressIcon = new ImageIcon("Icons/AddressBookIcon.jpg");

  EditorPaneFrame panel1 = Browser();

  //add tabs
  //tabbedPane.addTab("Address Book", addressIcon, panel1, "Contact info");
  tabbedPane.addTab("Browser", browserIcon, panel1, "Browse the web");

  panel = new JFrame();

  setVisible(true);
  }

  public EditorPaneFrame Browser()
  {
  EditorPaneFrame browser = new EditorPaneFrame();

  return browser;
  }

  public static void main(String[] args)
  {
     new CourserWindow();
  }
 }

y

package editorPane;  
 import java.awt.*;
 import java.awt.event.*;   
 import java.io.*;   
 import java.util.*;   
 import javax.swing.*;   
 import javax.swing.event.*;   
/** 
  * This frame contains an editor pane, a text field and button to enter a URL and     load a document, 
  * and a Back button to return to a previously loaded document. 
  */

public class EditorPaneFrame extends JPanel
{ 
       private static final int DEFAULT_WIDTH = 600; 
       private static final int DEFAULT_HEIGHT = 400; 
       public EditorPaneFrame() 
      { 
            setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT); 
            final Stack < String > urlStack = new Stack < >(); 
             final JEditorPane editorPane = new JEditorPane(); 
             final JTextField url = new JTextField( 30); 

            // set up hyperlink listener 
             editorPane.setEditable( false); 
             editorPane.addHyperlinkListener( new HyperlinkListener() 
                  { 
                         public void hyperlinkUpdate( HyperlinkEvent event) 
                        { 
                               if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
                              { 
                                     try 
                                    { 
                                          // remember URL for back button 
                                           urlStack.push( event.getURL(). toString()); 

                                          // show URL in text field 
                                           url.setText( event.getURL(). toString()); 
                                           editorPane.setPage( event.getURL()); 
                                    } 
                                     catch (IOException e) 
                                    { 
                                           editorPane.setText(" Exception: " + e); 
                                    } 
                              } 
                        } 
                  }); 

            // set up checkbox for toggling edit mode 
            final JCheckBox editable = new JCheckBox();
            editable.addActionListener( new ActionListener() 
                  { 
                        public void actionPerformed( ActionEvent event) 
                        { 
                               editorPane.setEditable( editable.isSelected()); 
                        } 
                  }); 

            // set up load button for loading URL 
             ActionListener listener = new ActionListener() 
                  { 
                         public void actionPerformed( ActionEvent event) 
                        { 
                               try 
                              { 
                                    // remember URL for back button 
                                     urlStack.push( url.getText()); 
                                     editorPane.setPage( url.getText()); 
                              } 
                               catch (IOException e) 
                              { 
                                     editorPane.setText(" Exception: " + e); 
                              } 
                        } 
                }; 
           JButton loadButton = new JButton(" Load"); 
           loadButton.addActionListener( listener); 
           url.addActionListener( listener); 

 // set up back button and button action 
           JButton backButton = new JButton(" Back"); 
           backButton.addActionListener( new ActionListener() 
                { 
                       public void actionPerformed( ActionEvent event) 
                      { 
                             if (urlStack.size() <= 1) return; 
                             try 
                            { 
                                  // get URL from back button 
                                   urlStack.pop();

                                 // show URL in text field 
                                   String urlString = urlStack.peek(); 
                                   url.setText( urlString); 
                                 editorPane.setPage( urlString); 
                          } 
                           catch (IOException e) 
                          { 
                                 editorPane.setText(" Exception: " + e); 
                          } 
                    } 
              }); 
         add( new JScrollPane( editorPane), BorderLayout.CENTER); 

        // put all control components in a panel 
         JPanel panel = new JPanel(); 
         panel.add( new JLabel(" URL")); 
         panel.add( url); 
         panel.add( loadButton); 
         panel.add( backButton); 
         panel.add( new JLabel(" Editable")); 
         panel.add( editable); 
         add( panel, BorderLayout.SOUTH); 

         setVisible(true);
  }      
 }

preguntado el 27 de noviembre de 13 a las 06:11

Para obtener una mejor ayuda antes, publique un SSCCE. As it is, that code won't compile, let alone throw a run-time exception. -

It compiles just fine but produces run-time errors -

1 Respuestas

In your editor pane you are extending JPanel and in constructor public EditorPaneFrame() you are adding a new JPanel's object in same JPanel.

add( panel, BorderLayout.SOUTH); 

Debes extend JFrame entonces funcionará.

respondido 27 nov., 13:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.