Litte Help with SpringLayouts
Im a bit confused why this code only shows panel 2 and not panel 1.
Code:
container = frame.getContentPane();
SpringLayout sl = new SpringLayout();
container.add(pnl1);
container.add(pnl2);
sl.putConstraint(SpringLayout.WEST, pnl1, 5,
SpringLayout.WEST, this);
sl.putConstraint(SpringLayout.NORTH, pnl1, 5,
SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.WEST, pnl2, 5,
SpringLayout.EAST, pnl1);
sl.putConstraint(SpringLayout.NORTH, pnl2, 5,
SpringLayout.SOUTH, pnl1);
If i try just removing panel2 (pnl2), pnl1 will show, but when put together its only pnl2 that is showing.
Ive also tried this for pnl2
Code:
layout.putConstraint (SpringLayout.WEST, pnl2,
10, SpringLayout.WEST, this);
layout.putConstraint (SpringLayout.NORTH, pnl2,
5, SpringLayout.SOUTH, pnl1);
but still no luck :(
Re: Litte Help with SpringLayouts
I just made a couple of changes on your code to make sure it's working, and it worked
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
public class Util extends JFrame {
private static final long serialVersionUID = -1869532151600910911L;
public Util() {
JLabel label1 = new JLabel("Panel1");
JLabel label2 = new JLabel("Panel2");
JPanel pnl1 = new JPanel();
pnl1.setSize(100, 100);
pnl1.setBackground(Color.RED);
pnl1.add(label1, BorderLayout.CENTER);
JPanel pnl2 = new JPanel();
pnl2.setSize(100, 100);
pnl2.setBackground(Color.YELLOW);
pnl2.add(label2, BorderLayout.CENTER);
Container container = getContentPane();
SpringLayout sl = new SpringLayout();
container.add(pnl1);
container.add(pnl2);
sl.putConstraint(SpringLayout.WEST, pnl1, 5, SpringLayout.WEST, this);
sl.putConstraint(SpringLayout.NORTH, pnl1, 5, SpringLayout.NORTH, this);
sl.putConstraint(SpringLayout.WEST, pnl2, 5, SpringLayout.EAST, pnl1);
sl.putConstraint(SpringLayout.NORTH, pnl2, 5, SpringLayout.SOUTH, pnl1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new Util();
}
}
2 Attachment(s)
Re: Litte Help with SpringLayouts
hanks for some reason my code still doesnt work :(
its now one better than before as i can see both panels but it seems that one goes on top of the other.
not sure why this is happening though....
the code ive used is also attached.
Thanks in advance