Computer Science Canada JFrame and Container question |
| Author: | jin [ Wed May 31, 2006 2:39 pm ] | ||
| Post subject: | JFrame and Container question | ||
hi when i run my program there are two output screens how do i make them into one?
i tried frame.getContentPane ().add (c); but i still had the same output. |
|||
| Author: | rizzix [ Wed May 31, 2006 3:22 pm ] |
| Post subject: | |
delete the "extends JFrame" |
|
| Author: | jin [ Wed May 31, 2006 4:08 pm ] |
| Post subject: | |
By removing the extends JFrame i get errors on these lines: super ("MAZE"); No applicable overload was found for a constructor of type "java.lang.Object". Perhaps you wanted the overloaded version "Object[];" instead? setSize (403, 420); No method named "setSize" was found in type "MAZE". show (); No method named "show" was found in type "MAZE". super.paint (g); No method named "paint" was found in type "java.lang.Object". application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); No method named "setDefaultCloseOperation" was found in type "MAZE". Any ideas on what i should do now. |
|
| Author: | rizzix [ Wed May 31, 2006 5:01 pm ] |
| Post subject: | |
Oh right... well in that case I don;t understand why you are creating a second frame (note how you have a "JFrame frame = new JFrame();") instead of adding the components to "this" frame. Actually I believe you don't know what you are doing. May I suggest you learn Java first, and then attempt to try Swing? |
|
| Author: | wtd [ Wed May 31, 2006 5:11 pm ] |
| Post subject: | |
He's right. If you don't truly understand what "extends" is about, then you have some more fundamental Java learning to do. |
|
| Author: | jin [ Wed May 31, 2006 6:55 pm ] | ||
| Post subject: | |||
Sorry i dont have a good teacher. and this is my original code. but for some reason the menu doesnt show until i change the frame dimension a bit and when i click on the "File" tab the menuitems appear but are behind the graphics (border in this case) until i pass over them. so i thought to make a new frame and then add the graphics to it. if u have any ideas on how to fix this code. plz let me know. thnx
|
|||
| Author: | HellblazerX [ Wed May 31, 2006 7:57 pm ] |
| Post subject: | |
I think you should keep the graphics portion and the JFrame separate, by placing the graphics inside a JPanel, and then adding the JPanel to the JFrame. |
|
| Author: | rizzix [ Wed May 31, 2006 8:13 pm ] |
| Post subject: | |
While this is much better, there is still quite a few problems, specifically coding related. Anyway I'll address only the Swing problems here. First: the "show();" at the top is depricated, use "setVisible(true);" instead. Secondly: only call "setVisible()", "show()" (now depricated) and "pack()" at the last line in your setup sub-routine, which in your case is the constructor itself. (Why we do this, is for efficiency reasons. That is, we call it only once, after we have finished creating the view, since a call to any of those three methods is deeply recursive to all the "added components" (or children) to that parent component.) So what you need to do is remove that "show()" and add a setVisible(true); at the very end of the constructor's body. |
|
| Author: | jin [ Wed May 31, 2006 8:57 pm ] |
| Post subject: | |
I changed the show() to setVisible(true) and moved it to the bottom now i am able to see the menu tabs but the problem still with the menu items still remains. I know how to add buttons, textarea, textfield, labels, etc. but not how to add graphics. can u show me an example to see how to do it. |
|
| Author: | jin [ Wed May 31, 2006 10:19 pm ] | ||
| Post subject: | |||
i read something on the website on how to add graphics to a panel and tried it but it did not work. i tried both adding it to a panel then to the container and straight to the container none of them worked.
is this the correct way that you meant when you said Quote: placing the graphics inside a JPanel, and then adding the JPanel to the JFrame. |
|||
| Author: | HellblazerX [ Thu Jun 01, 2006 4:12 pm ] | ||||||
| Post subject: | |||||||
What I meant was to create a separate container class that extends JPanel, have your graphics to be drawn in there, and then add that JPanel to the JFrame. The reason why the menu tabs are shown behind the maze is because when you click on them, repaint () is automatically called to draw that tab. You need to keep the repainting of the maze and the repainting of the menu tabs separate, which is why you place your graphics components in a separate JPanel. An example of this class would be this:
Also, this line:
You've made a new container to hold the picture, but this container does not apply at all to your JFrame. What it should look like is this:
This way, when you add things, they'll show up on your screen. |
|||||||
| Author: | jin [ Thu Jun 01, 2006 5:25 pm ] | ||
| Post subject: | |||
I made the changes but now the menu items do not show up at all. the tab gets selected but the list does not appear.
|
|||
| Author: | HellblazerX [ Thu Jun 01, 2006 5:52 pm ] | ||||
| Post subject: | |||||
It works fine when I ran it. Your problem might be that you didn't spell component right:
should be:
|
|||||
| Author: | jin [ Thu Jun 01, 2006 6:01 pm ] |
| Post subject: | |
Sorry bout that. stupid mistake on my part. java did not even give me an error.lol. thnx for all your help guys. Oh yea quick question is there a easier way to make all the contents expand or decrease with the screen size or do i have to set up a scale factor. |
|
| Author: | magicman [ Fri Jun 02, 2006 8:11 am ] | ||
| Post subject: | |||
i have a question about this, im doing the same thing basicyl... but i need help with one small thing. When i want a new game, what do i put so that the program will make a new one. Like i have something like this for exiting the program.
i think i know the answer to this question, but to make sure, will text show in the box it self, or will it make a new window, just for the text. If it does make a new window, how do i make it show up in the same window. |
|||
| Author: | wtd [ Fri Jun 02, 2006 8:27 am ] | ||
| Post subject: | |||
Well, look at the code.
Now, what part of this is actually exiting the program? If you can identify that, then you probably know where to put code to do something other than exiting the program. |
|||