Labels won't show in Frame
I've written a program with 4 classes:
- "Main" just creates an object of the JFrame in my Frame class.
- "Frame" creates the frame, visibility is true and I'm using "GameBar" and "Labels" classes to make a JMenuBar and a couple of JLabels.
- There are no errors that show up, so the code should be fine. But when I run it, it throws an Exception at me:
Code:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Main.main(Main.java:7)
Does anyone know what's going on? I mean, I can see what the exception is telling me, but I'm unsure as to what it means specifically. The frame is successfully created and it has a title, but it just can't seem to find the content inside my "GameBar" and "Labels" classes. But as I said, there's no errors popping up in any of the codelines. I'm using Eclipse.
Thanks in advance.
Re: Labels won't show in Frame
Can you post the code of your main method?
Re: Labels won't show in Frame
Quote:
Originally Posted by
awinston
Can you post the code of your main method?
Sure thing:
Code:
public class Main {
public static void main(String[] args) {
TDXFrame tdx = new TDXFrame();
tdx.add(new TDXFrame());
}
}
When I said "Frame" above, I meant "TDXFrame" (just shortened it down for this thread). Also, all the frame setters are inside the TDXFrame class, in case it's relevant. Essentially, the Main should run the Frame and the Frame should run the GameBar and Labels.
But only the frame itself is there, even though add codes have been correctly written (afaik, since no errors are showing) for GameBar and Labels.
Re: Labels won't show in Frame
Quote:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
The message says what the problem is.
Read the API doc for the Container class's addImpl() method.
Re: Labels won't show in Frame
I'm assuming TDXFrame extends JFrame, but correct me if I'm wrong.
You cannot add a top-level container(JFrame, JDialog, JWindow, JApplet) to another top-level container. In this case, you are trying to add a JFrame to a JFrame.
Re: Labels won't show in Frame
Ok guys, thanks for the replies. It looks like TDXFrame should be making a JPanel, not a JFrame. As you said, awinston, I was indeed making a JFrame contain a JFrame. :(giggle):
Re: Labels won't show in Frame
Moved from New to Java
db