Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applets

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.applet.*;
import java.awt.*;
public class Q20 extends Applet
{
Button okButton = new Button("Ok");
public void init() {
setLayout(new BorderLayout());
add("South", okButton);
add("North", okButton);
add("East", okButton);
add("West", okButton);
add("Center", okButton);

}
}
please tell me as to why the above applet behaves the way it does. I expect it to display five buttons positioned in five respective places. But only the 'Center' button is being displayed.
prasad
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct way is <CODE>add(Component c, Object Constraints)</CODE>
That translates to
add(okButton, "North")
although I prefer
add(okButton, BorderLayout.NORTH)
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony!
I was testing prasad's program and I came to know the foll.
The problem is not the syntax. Both add("North",bn) and
add(bn,GridBagLayout.NORTH) are correct
. This problem happens ONLY WHEN WE ADD THE SAME OBJECT again and again. It does not happen when we add diff objects it works fine. I created 5 diff btns and added them and it worked fine also.
The same this happens when we change the Layout to FlowLayout also. It appears as though when we add the same object again the layout manager just clears the old place... For simplicity I changed this as an application. The same thing happens in applet also.
Do you have any idea why this happens....

[I split one overlong line in the code - Jim]
[This message has been edited by Jim Yingst (edited February 02, 2000).]
 
paul wheaton
Trailboss
Pie
Posts: 21894
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The layout manager keeps track of all the objects added. Later you can call remove( component ) and it will remove the one component that you reference. So if you add the same component, it isn't really added, it sort of replaces the previous instance of the component.
 
venu gopal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maha,
I am browsing your posting in layouts. Your explanation was excellant. I have noticed a button was missing. It would be great if we could correct a line in your program.
import java.awt.*;import java.io.*;
public class mframe extends Frame{Button bn,bs,be,bw,bc;public mframe(){
super("maha");
Button bn = new Button("North");
Button bs = new Button("South");
Button be = new Button("East");
Button bw = new Button("West");
Button bc = new Button("Center");
setLayout(new BorderLayout());
/*add(bn);
add(bn);
add(bn);
add(bn);
add(bn); */
try{
add("North",bn);// If we comment out this line be // & bw resize vertically ok.
setSize(300,300);
setVisible(true);
System.out.println("Added West and sleepling");
Thread.sleep(2000);
add("East",be);
setSize(300,300);
setVisible(true);
System.out.println("Added East and sleepling");
Thread.sleep(2000);
add("South",bs); // I have corrected from bn to bs -- venu
setSize(300,300);
setVisible(true);
System.out.println("Added South and sleepling");
Thread.sleep(2000);
add("West",bw);
setSize(300,300);
setVisible(true);
System.out.println("Added West and sleepling");
Thread.sleep(2000);
add("Center",bc);
setSize(300,300);
setVisible(true);
System.out.println("Added Center and sleepling");
Thread.sleep(2000);
}catch(InterruptedException e) {} }
public static void main(String args[]){
new mframe();
}
}
[This message has been edited by venu gopal (edited January 12, 2001).]
[This message has been edited by venu gopal (edited January 12, 2001).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this discussion to Applets forum.
Ajitg
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic