Author Topic:   Frames
sree
ranch hand
posted April 19, 2000 01:12 PM             
I have a doubt regarding frames. Could somebody please clarify that.

Inorder to make a frame visible we have to give both setVisible() and setSize() right? But in the following example the frame is visible even after i commented the setSize(300,300) howcome???please clear this for me.




import java.awt.*;
class TestFrame extends Frame
{
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
TestFrame()
{
setLayout(new GridLayout(2,3));
//setSize(300,300);
Button b1 = new Button("Button1");
Button b2 = new Button("Button2");
Button b3 = new Button("Button3");
Button b4 = new Button("Button4");
Button b5 = new Button("Button5");
Button b6 = new Button("Button6");
Button b7 = new Button("Button7");
Panel p = new Panel();
Button b8 = new Button("hello from panel");
p.setBackground(Color.red);
p.setForeground(Color.green);
p.add(b8);

add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(p);

setVisible(true);
}
}



[This message has been edited by sree (edited April 19, 2000).]

[This message has been edited by sree (edited April 19, 2000).]

[This message has been edited by sree (edited April 19, 2000).]

satya5
ranch hand
posted April 19, 2000 01:46 PM             

As I understand, since the setVisible(true); is called,
the frame is visible. However, it's size is not set
(atleast on UNIX) since the SetSize() is commented out.

Also, please note that a Grid of 2x3 was defined in the
setLayout() call and 8 components (7 btns and a panel)
have been added. If I recollect RHE correclty, sometimes
this could lead to unpredictable results.

Do I make sense or did I get you wrong?

Hope this helps.

Regds.

- satya

sree
ranch hand
posted April 19, 2000 02:23 PM             
Satya thanks so much for your response.But i am still not clear.

Almost every book is saying that by default the frame is invisible. In order to make it visible we have to use either setBounds(),setVisible() or setSize(),setVisible() or pack(),setVisible() right??? Now here even when i just give setVisible() or show() without these size specifying methods i could still very well see the frame. I am little confused with this. Am i not able to understand something here.

Thanks a lot friends.

satya5
ranch hand
posted April 19, 2000 03:18 PM             

Try commenting out the setVisible() call and see
what happens. I ran the code you posted and as expected
I could see a small window pop-up on my screen
(I am on UNIX). The awt package uses the underlying
windowing system to a certain extent. I am not sure
of the details to what extent.....

This is what I observed :

When I comment out setVisible(true); call,
the frame is not visible at all.

When I comment out setSize(300,300); (the setVisible
call is now not commented) then the frame is visible
as a small window of zero size.

I am not sure of other method calls.

Regds.

- satya

girish
greenhorn
posted April 20, 2000 03:45 AM             
Sree,

When you use setVisible() for a frame without using setSize()
what happens is the frame is seen on left top in minimised form.
The setSize() gives it the size so that it is not in minimised
state when application starts.

This is what I understand.

Rgds
Girish

sree
ranch hand
posted April 20, 2000 11:25 AM             
If you don't call setVisible() and setSize() then the frame doesn't appear. That's what i read. But when i tried not giving setSize() the frame is still being displayed with zero size on the upper left corner of the screen. Then i could resize it. But that doesn't mean that frame is invisible. But when i tried not giving setVisible() then it is totally invisible.

Infact there is one question in R&H regarding this.

Your application constructs a frame by calling Frame f = new Frame(); but when you run the code,the frame does not appear on the screen.What code will make the frame appear?(choose one)

a)setSize(300,200);
b)f.setFont(new Font("serif",Font.BOLD,24));
c)f.setForeground(Color.white);
d)f.setVisible(true);
e)f.setSize(300,200);
f.setVisible(true);

The answer given is 'e' but even f.setVisible(true) itself alone is working.

Thanks.

Please clear this for me.

sree
ranch hand
posted April 20, 2000 03:24 PM             
Maha or Jim if it's not much of a trouble for you could you please clarify this for me. I am sorry i am bothering you.

Thanks.

maha anna
bartender
posted April 20, 2000 04:30 PM             
Sree,
When a frame is created, it has 0(zero) pixel width and 0(zero) pixel height. And is an invisible frame also.

The setSize(int w,int h) is related to setting the size of the frame. It is NOT related in any way to displaying the frame.

The setVisible(boolean b) is the method which is actually responsible for bringing the frame to our eyes.

So just setting the size alone WILL NOT make the frame visible.YOu have to give frameRef.setVisible(true);

So having said that, given some choices if we have to pickup which will make the frame appear like that, as you say just setVisible(true) alone will make the frame appear.

What I think is the author asks is , to pick up the more suitable answer from 5 of the above ans. And he specifically says 'Choose one' in Chapter 11 /page 341.

If this qstn would have been a multiple choice , then we can select both d) and e).

If there are some ansewers and only one answer has setVisible(true), and none of the other has the setVisible(true) as part of them, then we should select setVisible(true) only. Because this is the method which is responsible for making the frame visible.
regds
maha anna

sree
ranch hand
posted April 21, 2000 10:33 AM             
Thanks Maha.

|