Java JPanel Create createCenteredWindow(String title, Dimension size, JPanel panel, boolean exitOnClose)

Here you can find the source of createCenteredWindow(String title, Dimension size, JPanel panel, boolean exitOnClose)

Description

Create a simple centered window(JFrame).

License

Open Source License

Parameter

Parameter Description
title Window title.
size Window size.
panel Window contents panel.
exitOnClose If true closes the application

Return

New JFrame instance.

Declaration

public static JFrame createCenteredWindow(String title, Dimension size,
        JPanel panel, boolean exitOnClose) 

Method Source Code

//package com.java2s;
/**/*w  w w .  j  av a2  s .co  m*/
 * JFrameUtils
 * Created by Lu?s F. Loureiro, github.com/nczeroshift
 * Under MIT license
 */

import javax.swing.*;

import java.awt.*;

public class Main {
    /**
     * Create a simple centered window(JFrame).
     * @param title Window title.
     * @param size Window size.
     * @param panel Window contents panel.
     * @param exitOnClose If true closes the application
     * @return New JFrame instance.
     */
    public static JFrame createCenteredWindow(String title, Dimension size,
            JPanel panel, boolean exitOnClose) {
        JFrame frame = new JFrame(title);
        frame.setSize(size);
        frame.setPreferredSize(size);

        if (exitOnClose)
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        else
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);

        return frame;
    }
}

Related

  1. createBlankColorPanel(int height, int width, Color bkColor)
  2. createFlowJPanelLeft(JComponent parent, int alignment)
  3. createGridedJPanel(JComponent parent, int columns)
  4. createJPanel()
  5. createJPanel(String name, LayoutManager lm, ComponentListener cl)