Re: Layout (desperate) Help
pek wrote:
Please please please help me. I've tried every single Layout Manager
and a lot of combinations but cannot still make is work.
I created a prototype and uploaded a screenshot.
Here is how the panels should be placed in normal size:
http://pek.ekei.com/misc/help/exampleLayout_normal.png
Red boxes and buttons NEVER change size.
Yellow boxes expand left and right
and orange boxes expands everywhere.
Here is an example of the same window if resized to a bigger size:
http://pek.ekei.com/misc/help/exampleLayout_expanded.png
Please please help. I have no idea how to break this into smaller
panels and what LayoutManagers to use.
If possible, send me the code at my mail: p3k_me _at_ hotmail _dot_
com
Thank you very much.
Here's one way to do it using only nested BoxLayouts
package Misc;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class LayoutProblem extends JPanel {
LayoutProblem() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(Box.createRigidArea(new Dimension(0,5)));
JPanel row1Panel = new JPanel();
row1Panel.setLayout(
new BoxLayout(row1Panel, BoxLayout.LINE_AXIS));
for (int i = 0; i < 8; i++) {
row1Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row1Panel.add(makeRedBox());
}
row1Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row1Panel.add(Box.createHorizontalGlue()); // else boxes centred
add(row1Panel);
add(Box.createRigidArea(new Dimension(0,5)));
JPanel row2Panel = new JPanel();
row2Panel.setLayout(
new BoxLayout(row2Panel, BoxLayout.LINE_AXIS));
row2Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row2Panel.add(makeYellowBox(100));
row2Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row2Panel.add(makeYellowBox(100));
row2Panel.add(Box.createRigidArea(new Dimension(5, 0)));
add(row2Panel);
add(Box.createRigidArea(new Dimension(0,5)));
JPanel row3Panel = new JPanel();
row3Panel.setLayout(
new BoxLayout(row3Panel, BoxLayout.LINE_AXIS));
row3Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row3Panel.add(makeRedBox());
row3Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row3Panel.add(makeYellowBox(30));
row3Panel.add(Box.createRigidArea(new Dimension(5, 0)));
add(row3Panel);
add(Box.createRigidArea(new Dimension(0,5)));
JPanel row4Panel = new JPanel();
row4Panel.setLayout(
new BoxLayout(row4Panel, BoxLayout.LINE_AXIS));
row4Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row4Panel.add(makeOrangeBox());
row4Panel.add(Box.createRigidArea(new Dimension(5, 0)));
add(row4Panel);
add(Box.createRigidArea(new Dimension(0,5)));
JPanel row5Panel = new JPanel();
row5Panel.setLayout(
new BoxLayout(row5Panel, BoxLayout.LINE_AXIS));
row5Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row5Panel.add(makeRedBox());
row5Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row5Panel.add(makeYellowBox(30));
row5Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row5Panel.add(makeRedBox());
row5Panel.add(Box.createRigidArea(new Dimension(5, 0)));
row5Panel.add(makeButtonPanel());
row5Panel.add(Box.createRigidArea(new Dimension(5, 0)));
add(row5Panel);
add(Box.createRigidArea(new Dimension(0,5)));
}
JPanel makeRedBox() {
JPanel p = new JPanel();
p.setBackground(Color.RED);
Dimension d = new Dimension(30,30);
p.setPreferredSize(d);
p.setMaximumSize(d);
return p;
}
JPanel makeYellowBox(int height) {
JPanel p = new JPanel();
p.setBackground(Color.YELLOW);
p.setPreferredSize(new Dimension(height, height));
p.setMaximumSize(new Dimension(9999,height));
return p;
}
JPanel makeOrangeBox() {
JPanel p = new JPanel();
p.setBackground(Color.ORANGE);
Dimension d = new Dimension(100,100);
p.setPreferredSize(d);
return p;
}
JPanel makeButtonPanel() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.add(new JButton("Button"));
p.add(new JButton("Button"));
return p;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("");
f.add(new LayoutProblem());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
}
Of course, the buttons need to be made smaller but at this point I got
bored :-)