java JFrame sizes.
When I create a JFrame in Java, and use setSize to set the size in pixels, such as 600x600 I've noticed the frame doesnt constitue whats in the middle of the title bar, and outside of the whole WINDOW (aka the Window Manager stuff is part of the frame, and therefore doesnt ADD to the size, but rather the WHOLE windows is 600x600, which is not what I want.
I'm sure others have encountered this aswell, as I was wondering what the common way to work around this was.
Is this really a problem? You could try calculating the size of the menubar, the borders of the window and so on...
By some function or just counting all the little pixels? If the later is the case, what about people with bigass themes or differant window manager, operating systems?
Could you try something like this?
I'm new to java, but heres my code and what I tried to change with your suggestion/
public class DeathSoccer extends JPanel implements ActionListener
public static void main(String args) // why static???
JFrame theFrame = new JFrame("Death Soccer");
I'll still need some help, Thanks.
In Java, everything's a class (or just about everything).
You can "draw()" a "circle object"; a circle object "has a" radius and "has a" color (both "color" and "radius" being examples of "instance data"), etc etc.
But what happens if you want to declare a variable, or have a method, that's *independent* of any particular object?
That's what the (non-intuitively named) "static" keyword is for in Java: to denote some data item or some operation that doesn't belong to any particular object, but instead to the class as a whole.
Declaring a method "public static void main (String args)" is the way to tell the Java runtime "If I tell you to 'run this class', this is the place I want you to begin program execution'".
And guess what - you can have different "public static void main ()" methods in different classes - unlike C/C++, you *don't* have to have just one "main()" in your entire program. That's a neat way to create and drive per-class unit tests.
1. The point in the code above is:
a) You have a "JFrame" as your "main window": it has the title bar, it holds your menu bar and tool bars, etc etc
b) It's also a "container" for your user interface
c) As such it has a layout manager (whether or not you explicitly specify one!) that organizes its contents
d) Your "user interface" consists of organizing UI objects - and other containers - inside your JFrame
e) It's a much more sophisticated model than, say, Visual Basic (where you just slap button #1 at x/y 100, 10). The
Java model is much more flexible, and much better at automatically handling different screen resolutions and sizes.
2. A couple of good resources include:
a) Sun'sJava tutorials:
b) "Just Java", Peter van der Linden
<= AN EXCELLENT BOOK ABOUT JAVA, AN EXCELLENT INTRO TO OO PROGRAMMING
c) "Head First Java", Kathy Sierra/Bert Bates
<= MAY OR MAY NOT BE TO YOUR TASTES:
LOOK AT BOTH "JUST JAVA" AND "HEADFIRST JAVA", AND TAKE THE ONE THAT APPEALS MORE
... and ...
d) Java Ranch:
<= AN EXCELLENT SITE FOR POSTING QUESTIONS
'Hope that helps .. PSM
There is the full file of my main class.
When I tried your approach, I got it to compile and a window showed up. I took a raw screenshot of it, and used to gimp to copy everything that was in the middle of my window manager stuff, and it was 610,610 pixels. I dont understand why that would be.
Regardless, thank you for that last post. I must admit to this being a homework assignment (or rather were just supposed to be animating various graphics objects, but I implement all the events and mouse stuff etc etc etc later, and just animate some of the stuff in this 'game') but I'd like to get this stuff all 'aligned'.
Now why would this extend JFrame, and why would mine extend JPanel? In addition even though it was 610x610, why would my graphics stuff not show up? Now in my CURRENT code, when all my objects draw the graphics, they are drawing them on a Panel, correct? What panel would that be? is DeathSoccer (which extends JPanel) a panel itself?
This is only my second or thrid java program, and needless to say I've come from two intro courses on C++.
Thank you again.
I'm not sure what would account for the 600 vs 610 pixel discrepency - I've certainly never noticed any such thing myself.
If you're interested, there's a very good tutorial on the Sun page:
They talk about "Container insets" - I wonder if maybe these "insets" might be the culprit.
Anyway - good luck with your studies! And please do check out some of the resources I mentioned!
Sincerely .. PSM
Will do. My professor had recommened looking into a feature of the java lanquage that can "intergate the os/system for the window manager dimensions". I havent looked into it that much, and frankly your solution seems much more plausible, but I would think it would be in something like ToolKit. Regardless thank you very much.
|All times are GMT -5. The time now is 07:18 AM.|