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

Layouts

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
import java.awt.*;
import java.awt.event.*;
class btn {
public static void main(String s[]){
Frame w = new Frame();
Panel p = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Button b = new Button("ok");
Button b1 = new Button("cancel");
Button b2 = new Button("exit");

li l= new li();
b.addActionListener(l);
w.setLayout(new GridLayout(3,1));
w.add(p);
w.add(p1);
w.add(p2);
p2.setBackground(Color.blue);
p2.setLayout(new FlowLayout());
p2.add(b);
p2.add(b1);
p2.add(b2);

p1.setBackground(Color.green);
p1.setLayout(new FlowLayout());
p1.add(b);
p1.add(b1);
p1.add(b2);

p.setBackground(Color.red);
p.setLayout(new FlowLayout());
p.add(b);
p.add(b1);
p.add(b2);
w.setVisible(true);
w.setSize(200,200);

}
}
class li implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("hai");
}
}
</pre>
I just want to dispaly three panels horizontally with diff colors
and each panel must have three btns arranged left to right.
But i am getting in only one panel ie last one.How to rectify it??
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really a Swing / AWT question, so I'm moving the post to that topic.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want three different sets of buttons to appear, you need to create three different sets of buttons using new Button(). Right now you're only creating one set of buttons and trying to add it to three different panels, which doesn't work. Each component you create should only be added once to a container. If you want nine buttons, there should be nine new Button() statements. (And nine button.addActionListener() statements.)
Since all three of you panels are nearly identical except for color, you may also want to create them using a loop, or even define a separate class ColoredPanel which you create three times. In that case you'd only write three new Button() statements (one for each button), but those statements would be executed three times, for nine total.
Also, the setVisible() still isn't quite the last statement in the constructor, so you've still got display problems until the user resizes the Frame. Make setVisible() the very last thing in the constructor; it will work better.
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic