Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes I can't seem to get the hang of JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "I can Watch "I can New topic
Author

I can't seem to get the hang of JFrame

Joris Verbaenen
Greenhorn

Joined: Jan 26, 2012
Posts: 1
Hi everybody,

Seven years ago, I got some Java course at the university. Now, with some free time, I'm trying to pick things up again and I'm programming a (little) game. Somebody wrote a nice tutorial about it, and I'm trying to follow it. But I can't seem to get the hang of JFrame.

I would like to have a JFrame. I define different classes, each representing a different screen (a starting screen, a playing screen, losing- or winning screen). But, when my program runs, I just have an empty JFrame. None of the labels is showing.

All of the "screen"-type classes have the same structure, so I just show you my starting class ("applicationmain") and the "startingscreen" class.

ApplicationMain:



and the StartScreen class:



I would appreciate any help! Looking already two evenings at these classes and can't seem to find my problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, yeah. These two lines:

are definitely going to create a frame with nothing in it. I don't know what that "Screen" thing is but whatever it is, you don't ever add it to your frame. And even if you did, it wouldn't have any effect because those two lines of code make a frame which doesn't have room for anything else.

And this code:


You shouldn't be changing things on the screen every time you repaint the frame. The repaint method will be called every time Swing decides it needs to redraw your GUI. Like when you minimize it and restore it, when you put another application in front of it, and many other times.

And calling "removeAll" isn't going to do anything to a JFrame which didn't have any components added to it in the first place.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3595
    
  14

Welcome to CodeRanch, Joris
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I think it is quite ambitious for someone who took programming 7 years ago and is just playing around with it to start with a UI app, but perhaps there are other things you did first.

I don't know what you have in mind for the Screen class; you don't say. I don't know whether you intend for all the windows/frames you talk about to appear in their own windows, or to all appear in the same place in one main window as things change, etc.

I do notice that you have implemented a repaint() method that removes everything from a content pane, and then puts things back, which doesn't sound like what you are likely to have meant. Let me offer, as a principle, that it's rather unusual to have to override repaint at all, and gets into complexities that perhaps you should leave until you have more of the basics down.

So, your main method instantiates ApplicationMain. AM's constructor creates another JFrame, does not put anything on this second one except a title, packs it, and then creates a StartScreen object. It doesn't have a constructor, so constructing it doesn't put anything on any frame or panel. I think you must have meant for repaint to get called after that, but I don't think it is -- repaint is a method on MainApplication, which is never made visible, and I don't want to try to figure out what happens when it is. So I think nothing gets displayed (except perhaps a frame, I haven't worked that out exactly).

I think you need to take a couple of steps back and form an overall picture of what you want to do, THEN try to translate it into Swing components and operations, one step at a time.

JFrame can be thought of as a basic container for a main window. You can extend it in a class and then call JFrame methods on that class (adding controls, making it visible), OR you can declare a variable of some sort to be of type JFrame and call all those same methods on it. You don't need to do both unless you are going to have more than one "main window".

You seem to have already grasped that the content pane is the one where things get added to the JFrame, very good.

I suggest doing some simpler things with JFrame and getting them to work before embarking on your four-screen game. Put up some labels and buttons and have the button actions do things with the labels, or something. Or put up your FIRST game window, with all the controls you want on it, and make it visible even though it doesn't do much. THEN start adding responses to keystrokes and changing to other windows and so forth, in small(er) pieces.

You will discover that a JFrame's default layout manager (another concept you'll need to get familiar with, if you aren't already) is BorderLayout, and adding a component to it without specifying where in the BorderLayout it goes is going to put it in the Center. Adding two things there in succession will have the second one replace the first, I believe, so although you add two labels I think only the second will appear.

I will leave you with this as something to build on:



As an exercise, I leave it to you to make the labels appear with a north-south orientation instead of east-west. You might want to look at "BoxLayout" and figure out how to use it for the purpose.

rc
 
Consider Paul's rocket mass heater.
 
subject: I can't seem to get the hang of JFrame
 
Similar Threads
Icons and labels?
I can't load a file with getResource
Would my code help?!
Icons and a Label
Java Software Solutions Graphics Example 3.9 noob help