• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Marcus exam Q12

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q12.What best describes the appearance of an application with the following code?
import java.awt.*;
public class FlowAp extends Frame{
public static void main(String argv[]){
FlowAp fa=new FlowAp();
fa.setSize(400,300);
fa.setVisible(true);
}
FlowAp(){
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
}//End of constructor
}//End of Application

1) A Frame with buttons marked One to Four placed on each edge.
2) A Frame with buutons marked One to four running from the top to bottom
3) A Frame with one large button marked Four in the Centre
4) An Error at run time indicating you have not set a LayoutManager

The answer is 3.
The default layout manager for a Frame is the BorderLayout manager. This Layout manager defaults to placing components in
the centre if no constraint is passed with the call to the add method.I understood this.But what happens to the other three buttons?Can anyone help me?
Thanks
Sanju
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is actually happening is that you are putting one button on top of the other so the end result is you only see one
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other three get lost through the cracks... Only the last add() prevails...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is you are not specifying any direction for any button...and default direction for Border is at center..all the buttons will be placed on each other..only last will be visible.
Aamir
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to java. I am having trouble modifing the code so you CAN see all four buttons. I understand that there are four buttons on top of each other. I want to modify the code so that they are displayed similar to cascaded windows.

Thanks in advance
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Give it the Flow layout manager (might have something on that in my most excellent certification tutorial)
 
Keith Walburn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to click on Cup #8 of tutorial and that link took me to a blank page. All of the other links worked fine.
Thanks
 
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
Hi,
Tony is absolutely right when he says
The other three get lost through the cracks... Only the last add() prevails...
i have written the code below to check this out.
Basically i have added actionListeners for all the buttons. These are the three case scenario's i tried out.
1) I expected to see the third button when i set the fourth button setVisible to false. However the screen was blank.
2) Now i set the second button visibility to true and expected to see this. but this too did not take place
3) I then added the second button to the frame expecting to see it. This too did not take place.
What is happening!!!.
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
public class FlowAp extends Frame implements ActionListener{
public static void main(String argv[]){
FlowAp fa=new FlowAp();
fa.setSize(400,300);
fa.setVisible(true);
}
Button two;
FlowAp(){
Button one=new Button("One");
two=new Button("Two");
Button three=new Button("Three");
Button four=new Button("Four");
add(one);
add(two);
add(three);
add(four);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object a=e.getSource();
Button x=(Button)a;
x.setVisible(false);
two.setVisible(true);// this does not work
add(two);// this does not work
repaint();
}
//End of constructor
}//End of Application
I think that one cannot add objects to containers like frames dynamically.
Containers like frames and panel's maintain the Objects they contain in a collection. thus when i add a new button in the center the old components in the center are removed from the collection and replaced by the new one. thus the third button is not there in the collection anymore so is not seen when i set the Button four Visiblity to false.
Keith,
I think u ought to use the CardLayout.
Regards
Rahul.

[This message has been edited by rahul_mkar (edited July 09, 2000).]
 
Keith Walburn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I hava a better understanding of it now.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please tell me where can i find the mock exams by Marcus Greene
 
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
hi Manish,
when ever u do want anything u can try out the search facility provided and u will get the desired results. any how here i the link to marcas's site http://www.jchq.net/
Regds.
Rahul

[This message has been edited by rahul_mkar (edited July 11, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic