Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

BorderLayout in Applets

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:
From Kumar mock
: import java.applet.*;
2: import java.awt.*;
3:
4: public class Q20 extends Applet
5: {
6: Button okButton = new Button("Ok");
7:
8: public void init()
9: {
10: setLayout(new BorderLayout());
11:
12: add("South", okButton);
13: add("North", okButton);
14: add("East", okButton);
15: add("West", okButton);
16: add("Center", okButon);
17:
18: setSize(300,300);
19: }
20: }
The above Applet will display
A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet.
B) Only one Button with label "Ok" at the Top of the Applet.
C) Only one Button with label "Ok" at the Bottom of the applet.
D) Only one Button with label "Ok" at the Center of the Applet.
My question:
Why the answer is D and the applet didn't respect the new BorderLayout ?
Thx in advance
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the BorderLayout. Note that you are creating only one Button object and trying to add the same in all five positions!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is because the border layout is not using the location strings at all so the button ends up in the default center position.
Try using the add method that uses (Component, constraints) and using the String constants for the constraints.
add( okButton, BorderLayout.NORTH ) ; etc.
The add method that uses (String name, Component c ) is what was used in JDK 1.02 - it has not been deprecated but the current JDK strongly suggests using the other one.
Bill

------------------
author of:
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...it doesn't seem to be taking the default center location. It seems to depend on the order in which the button have been added in the init() method. If add( okButton, BorderLayout.NORTH ) ; had been the last addition okbutton appears at the north of the applet
------------------
lakshmi
 
Hang a left on main. Then read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!