Question : SpringLayout problem

Why doesn't the button appear?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
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);
	}
	
}

Answer : SpringLayout problem

Sorry - you do.

If you want to use an extra panel, you'll have two layout managers, so you need to ensure that the bottom one allows the top one to be visible. Try the following as the first line of your ctor
1:
setLayout(new java.awt.BorderLayout());
Random Solutions  
 
programming4us programming4us