• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Frames

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
<pre>


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);
}
}


</pre>
[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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maha.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic