gpt4 book ai didi

java 。线程中的异常 "main"java.lang.IllegalArgumentException : adding a window to a container

转载 作者:行者123 更新时间:2023-12-01 13:47:42 28 4
gpt4 key购买 nike

当我尝试运行我的程序时,不断收到以下错误:

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)

这是我的文件:

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();
}
}

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

最佳答案

在编辑器 Pane 中,您正在扩展 JPanel 并在构造函数中public EditorPaneFrame() 您正在同一 JPanel 中添加一个新的 JPanel 对象。

add( panel, BorderLayout.SOUTH); 

您应该扩展 JFrame 然后它就会起作用。

关于 java 。线程中的异常 "main"java.lang.IllegalArgumentException : adding a window to a container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235030/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com