javaswingjframe

Why button is filling up the whole JFRAME


I have make this demo testing code but when ever i run this code the button covers whole frame and i have even tried SetBound method but still it do not work

package com.Testing.Java;
import javax.swing.*;
import java.awt.*;

public class Main {

    public static void Simple(){
        JFrame f=new JFrame();

        JButton b=new JButton("Click");
        b.setBounds(100,100,100,100);


        f.add(b);
        f.setSize(400,500);
        f.setVisible(true);
        f.setLayout(null);
    }

    public static void main (String args[]){
        Simple();
    }
}

Solution

  • first don't use null layout .use layout managers

    default frame layout is border layout .so button is added to center and use entire frame.you add button before set layout.

    when you add first set layout then add component

    like this

    f.setLayout(null);
    f.add(b);