LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java JFrame sizes. (http://www.linuxquestions.org/questions/programming-9/java-jframe-sizes-374511/)

byteframe 10-18-2005 10:05 PM

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.

Thank you,

mrcheeks 10-18-2005 10:45 PM

Is this really a problem? You could try calculating the size of the menubar, the borders of the window and so on...

byteframe 10-18-2005 11:10 PM

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?

paulsm4 10-18-2005 11:53 PM

Could you try something like this?
Quote:

jFrame ()
<= THIS INCLUDES THE WHOLE SHEBANG, INCLUDING MENUBAR ETC
setLayout (flowLayout)
<= THIS WILL EXPAND TO INCLUDE WHATEVER IT CONTAINS
add JPanel ()
jPanel.setSize (600, 600)
<= NOW YOUR ENTIRE FRAME SHOULD WRAP AROUND A 600x600 WINDOW
jFrame.setVisible (true)
I'm not sure I understand the problem, but I hope that might help a bit...

byteframe 10-19-2005 12:24 AM

I'm new to java, but heres my code and what I tried to change with your suggestion/



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DeathSoccer extends JPanel implements ActionListener
{

public static void main(String[] args) // why static???
{
JFrame theFrame = new JFrame("Death Soccer");
theFrame.setLayout(flowLayout);
DeathSoccer.setSize(600,600);

theFrame.getContentPane().add(new DeathSoccer());

theFrame.setResizable(false);

theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//theFrame.setSize(600,600);

theFrame.setLocation(0,0);

theFrame.setVisible(true);
}



I'll still need some help, Thanks.

paulsm4 10-19-2005 01:51 AM

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class test extends JFrame {

  public static void main(String[] args) // why static???
  {
    JFrame theFrame = new JFrame ("test");
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    theFrame.setLayout(new FlowLayout ());
    JPanel thePanel = new JPanel ();
    thePanel.setPreferredSize(new Dimension (600,600));
    theFrame.add(thePanel);
    theFrame.pack ();

    theFrame.setVisible(true);
  }
}

Is this the only way to do it? Of course not. The best way? That's for you to decide. What are the alternatives? That's for you to play with and find out for yourself.

Why static?

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.

ANYWAY:
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:
http://java.sun.com

b) "Just Java", Peter van der Linden
http://www.bookpool.com/sm/0131482114
<= AN EXCELLENT BOOK ABOUT JAVA, AN EXCELLENT INTRO TO OO PROGRAMMING

c) "Head First Java", Kathy Sierra/Bert Bates
http://www.bookpool.com/sm/0596009208
<= 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:
http://www.javaranch.com
<= AN EXCELLENT SITE FOR POSTING QUESTIONS

'Hope that helps .. PSM

byteframe 10-19-2005 04:36 AM

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DeathSoccer extends JPanel implements ActionListener
{
    field soccerField;
    board scoreBoard;
    meter forceMeter;
   
    ball soccerBall;
    defender[] soccerDefense = new defender[3];
    goalie soccerGoalie;
   
    public static void main(String[] args) // why static???
    {
        JFrame theFrame = new JFrame("Death Soccer");

        theFrame.getContentPane().add(new DeathSoccer());

        theFrame.setResizable(false);

        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.setSize(600,600);

        theFrame.setLocation(0,0);

        theFrame.setVisible(true);
    }
   
    public DeathSoccer()

    {

        Timer t = new Timer(50,this);

        t.start();
       
        soccerField = new field();
        scoreBoard = new board();
        forceMeter = new meter();
       
        soccerBall = new ball();   
        for(int cnt = 0; cnt < soccerDefense.length; cnt++)
        {
            soccerDefense[cnt] = new defender((cnt*50),200);
        }
        soccerGoalie = new goalie();

    }
   
    public void paintComponent(Graphics g)

    {

        super.paintComponent(g);


        g.setColor(Color.gray);

        g.fillRect(0,0,600,600); // master background

        soccerField.drawField(g);
        scoreBoard.drawBoard(g);
        forceMeter.drawMeter(g);
       
        soccerBall.drawBall(g);
        for(int cnt = 0; cnt < soccerDefense.length; cnt++)
        { 
            soccerDefense[cnt].drawDefender(g);
        }
        soccerGoalie.drawGoalie(g);

    }
   
    public void actionPerformed(ActionEvent e)
    {       
        repaint();
    }
}


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.

paulsm4 10-24-2005 05:35 PM

Hi -

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:

http://java.sun.com/developer/online...e.html#whyNeed

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

byteframe 10-25-2005 02:09 AM

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 04:26 PM.