Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.swing.*;

import java.awt.*;

public class Main {
    public static void createAndShowGUI(final String titile, final JPanel pane) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame(titile);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                pane.setOpaque(true);
                frame.setContentPane(pane);
                frame.pack();
                // frame.setSize(width, height);
                frame.setVisible(true);
                moveToCenter(frame);
            }
        });

    }

    public static void moveToCenter(JFrame frame) {
        int windowWidth = frame.getWidth();
        int windowHeight = frame.getHeight();
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        frame.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);
    }
}