fuin.org

Small Open Source Java Tools and Libraries

FramePositioner

Do you just want to test that newly created "JPanel" and show it centered on the screen?
If you use Utils4Swing and the FramePositioner this will require only one line of code:
/**
 * Example panel.
 */
public final class ExamplePanel extends JPanel {

    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public ExamplePanel() {
        super();
        setPreferredSize(new Dimension(400, 300));
        setLayout(new BorderLayout());
        add(new JLabel("Hello world!", JLabel.CENTER), BorderLayout.CENTER);
    }

    /**
     * Starts the panel for a short test.
     *
     * @param args
     *            Not used.
     */
    public static void main(final String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                // Initialize the system LnF without ugly "try catch"
                Utils4Swing.initSystemLookAndFeel();

                // Create a frame using the "preferred size" of the panel and
                // show it centered on the screen with a single line of code...
                Utils4Swing.createShowAndPosition("Example", new ExamplePanel(), true,
                        new ScreenCenterPositioner());

            }
        });

    }