2

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();
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Welcome to Stack Overflow. There's no need to use `` - just indent the code (as you already had) and Markdown will take care of the rest. – Jon Skeet Mar 18 '16 at 13:31
  • 1
    default frame layout is border .so button is added to center and use entire frame.you add button before set layout – Madhawa Priyashantha Mar 18 '16 at 13:31
  • 1
    The default layout of the `JFrame` is `BorderLayout`. Your button gets added to `BorderLayout.CENTER`, so it takes the whole space. http://stackoverflow.com/questions/8660751/java-swing-jframe-layout – Arnaud Mar 18 '16 at 13:32
  • 1
    thanks that worked :) – Jatin Kathuria Mar 18 '16 at 13:34

1 Answers1

2

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);
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60