|
-
Mar 26th, 2008, 11:07 PM
#1
Thread Starter
Hyperactive Member
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
Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered 
If someone helped you today then please consider rating their post.
-
Mar 27th, 2008, 02:03 AM
#2
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();
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Apr 12th, 2008, 08:45 PM
#3
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|