allelopath
asked on
SpringLayout problem
Why doesn't the button appear?
package springlayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
public class SpringLayoutTest extends JPanel{
public SpringLayoutTest() {
SpringLayout mySpringLayout = new SpringLayout();
JPanel myPanel = new JPanel(mySpringLayout);
JButton helloButton = new JButton("hello");
myPanel.add(helloButton);
mySpringLayout.putConstraint(SpringLayout.WEST, helloButton, 10, SpringLayout.WEST, myPanel);
mySpringLayout.putConstraint(SpringLayout.NORTH, helloButton, 10, SpringLayout.NORTH, myPanel);
this.add(myPanel);
}
/**
*
* @param s
*/
public static void main(String s[]) {
JFrame frame = new JFrame("SpringLayoutTest");
SpringLayoutTest springLayoutTest = new SpringLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(springLayoutTest);
frame.setSize(300, 300);
frame.setLocation(300,300);
frame.setVisible(true);
}
}
ASKER
That's not what I'm looking to do. I want a JPanel that contains a JPanel that is using SpringLayout.
OK. Just use your code, but do
myPanel.setLayout(mySpringLayout);
ASKER
Does this accomplish that?
JPanel myPanel = new JPanel(mySpringLayout);
Yes, that'll do it
ASKER
I already have that, in the original code
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That works, but I don't understand why. JPanel has FlowLayout as a default layout manager. Why do I have to explicitly set a layout manager?
It's a question of how the subpanel behaves as far as sizing is concerned
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window