Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TestPane center = new TestPane(100, 100);
        frame.add(center);
        frame.add(new TestPane(100, 50), BorderLayout.NORTH);
        frame.add(new TestPane(100, 50), BorderLayout.SOUTH);
        frame.add(new TestPane(50, 100), BorderLayout.EAST);
        frame.add(new TestPane(50, 100), BorderLayout.WEST);

        System.out.println("Size beofre pack = " + frame.getSize() + "; " + center.getSize());

        frame.pack();

        System.out.println("Size after pack = " + frame.getSize() + "; " + center.getSize());

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class TestPane extends JPanel {
    int pWidth, pHeight;

    public TestPane(int width, int height) {
        pWidth = width;
        pHeight = height;
        setBorder(new LineBorder(Color.RED));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(pWidth, pHeight);
    }
}