• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Layout Problems

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I'm facing a layout problem and I have to make the applet ready and give it to my boss urgently. So help me out !!! Following is the code. I have created three panels(all with GridLayout) and my applet also have GridLayout. I have added the panels on the applet and they are ok. The problem is with two buttons. I wanna add them below the three panels, in the center. Thats the problem and I'm not able to solve it since I'm not very proficient with layout managers. Help me out!!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Rest extends Applet {
Checkbox c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12;
Panel p1,p2,p3,p4;
Label l1,l2,l3;
Button b1,b2;
Label l4,l5,l6;
public void init()
{
setLayout(new GridLayout(1,3));
l1=new Label("PIZZAS");
l2=new Label("FAST FOOD");
l3=new Label("BURGERS");
l4=new Label("");
l5=new Label("");
l6=new Label("");
b1=new Button("SUBMIT");
b2=new Button("CANCEL");
c1=new Checkbox("1");
c2=new Checkbox("2");
c3=new Checkbox("3");
c4=new Checkbox("4");
c5=new Checkbox();
c6=new Checkbox();
c7=new Checkbox();
c8=new Checkbox();
c9=new Checkbox();
c10=new Checkbox();
c11=new Checkbox();
c12=new Checkbox();
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
pp=new Panel();
p1.setLayout(new GridLayout(5,0));
p1.add(l1);
p1.add(c1);
p1.add(c2);
p1.add(c3);
p1.add(c4);
p2.setLayout(new GridLayout(5,0));
p2.add(l2);
p2.add(c5);
p2.add(c6);
p2.add(c7);
p2.add(c8);
p3.setLayout(new GridLayout(5,0));
p3.add(l3);
p3.add(c9);
p3.add(c10);
p3.add(c11);
p3.add(c12);
p4.setLayout(new GridLayout(1,5));
p4.add(l4);
p4.add(l5);
p4.add(b1);
p4.add(l6);
p4.add(b2);
add(p1);
add(p2);
add(p3);
setLayout(new FlowLayout());
add(p4);
}
}
Thanks in advance
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this

There's not a lot different. I just added another panel and used GridLayout for the applet itself. You'll notice that is seems more 'centered' at different sizes. This is a result of using the flowlayout in the button's panel. You can resolve this by figuring out the size that looks good and then adding this,
this.setSize( bestWidth, bestHeight );
this.setResizable( false );
If you call this applet from an html page the size parameters in the html will override these, so make sure the html params are the same. You might have been able to make this more accurately using the GridBagLayout but, personally, I would simply set the layout to null in the applet, add the panel containing the checkboxes and then set the bounds of the button panel using x and y variables that are calculated proportionally to the checkbox panel. Also, I would have the button panel adjust dynamically in the repaint method. This way they will get repositioned when the applet is resized. I hope this helps.
Sean
 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic