import java.awt.Dimension;import javax.swing.BorderFactory;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SpringLayout;public class SpringLayoutBug extends JFrame { /** * */ private static final long serialVersionUID = 1L; private SpringLayout layout = new SpringLayout(); private JPanel borderedJPanel = new JPanel(); private JPanel plainJPanel = new JPanel(); private JLabel titleJLabel = new JLabel("This JLabel will hold the title for this window."); public SpringLayoutBug(int width, int height, boolean jFrame) { this.setSize(new Dimension(width, height)); this.setPreferredSize(new Dimension(width, height)); // Everything is added and references the JFrame. if(jFrame) { this.setLayout(this.layout); // Tell SpringLayout to center the JLabel horizontally when the JFrame expands. layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, this.titleJLabel, 0, SpringLayout.HORIZONTAL_CENTER, this); // Tell SpringLayout to keep the JLabel at the top of the JFrame. layout.putConstraint(SpringLayout.NORTH, this.titleJLabel, 0, SpringLayout.NORTH, this); // Add the JLabel to the JFrame. this.add(this.titleJLabel); // Give the JPanel a border so we can see it. borderedJPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "EtchedTitleBorder")); // Tell SpringLayout to keep the JPanel 25 px from the left edge of the JFrame. layout.putConstraint(SpringLayout.WEST, this.borderedJPanel, 25, SpringLayout.WEST, this); // Tell SpringLayout to keep the JPanel just below the JLabel. layout.putConstraint(SpringLayout.NORTH, this.borderedJPanel, 25, SpringLayout.SOUTH, this.titleJLabel); // Tell SpringLayout to keep the bottom of the JPanel 100 px away from the bottom of the JFrame. layout.putConstraint(SpringLayout.SOUTH, this.borderedJPanel, -100, SpringLayout.SOUTH, this); // Tell SpringLayout to keep the right side of the JPanel 50 px away from the right side of the JFrame. layout.putConstraint(SpringLayout.EAST, this.borderedJPanel, -50, SpringLayout.EAST, this); // Add the JPanel to the JFrame. this.add(this.borderedJPanel); } else { // Everything is instead added to another JPanel, which is then added to the JFrame. // Set the JPanel to which everything will be added to use the SpringLayout. plainJPanel.setLayout(this.layout); // Tell SpringLayout to center the JLabel horizontally when the JFrame expands. layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, this.titleJLabel, 0, SpringLayout.HORIZONTAL_CENTER, this.plainJPanel); // Tell SpringLayout to keep the JLabel at the top of the JPanel. layout.putConstraint(SpringLayout.NORTH, this.titleJLabel, 0, SpringLayout.NORTH, this.plainJPanel); // Add the JLabel to the JFrame. plainJPanel.add(this.titleJLabel); // Give the JPanel a border so we can see it. borderedJPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "EtchedTitleBorder")); // Tell SpringLayout to keep the JPanel 25 px from the left edge of the JPanel. layout.putConstraint(SpringLayout.WEST, this.borderedJPanel, 25, SpringLayout.WEST, this.plainJPanel); // Tell SpringLayout to keep the JPanel just below the JLabel. layout.putConstraint(SpringLayout.NORTH, this.borderedJPanel, 25, SpringLayout.SOUTH, this.titleJLabel); // Tell SpringLayout to keep the bottom of the JPanel 100 px away from the bottom of the JPanel. layout.putConstraint(SpringLayout.SOUTH, this.borderedJPanel, -100, SpringLayout.SOUTH, this.plainJPanel); // Tell SpringLayout to keep the right side of the JPanel 50 px away from the right side of the JPanel. layout.putConstraint(SpringLayout.EAST, this.borderedJPanel, -50, SpringLayout.EAST, this.plainJPanel); // Add the bordered JPanel to the plain JPanel which will be added to the JFrame. plainJPanel.add(this.borderedJPanel); // Add the plain JPanel (with the components) to the JFrame. this.add(this.plainJPanel); } } public static void main(String[] args) { // Sending in a 'true' will display the bug. // Sending in a 'false' will cause it to display correctly through a 'workaround'. JFrame testFrame = new SpringLayoutBug(600,600, false); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); testFrame.setLocationRelativeTo(null); testFrame.setVisible(true); }}