I tried using BorderLayout as in label but does not work

package HelloWorldGUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class HelloWorldGui {

    private JFrame a;
    private JPanel p;
    private JButton b1;
    private JLabel lab;

    public HelloWorldGui() 
    {

        gui();
    }
    public void gui()   {
        a = new JFrame("window");
        a.setVisible(true);
        a.setSize (300,200);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p = new JPanel();
        p.setBackground(Color.WHITE);

        b1 = new JButton("Exit");
        lab = new JLabel ("Hello World!");
        b1.addActionListener(new ActionListener(){


            public void actionPerformed(ActionEvent event) {
               System.exit(0);
        }
        });

        p.add(b1);
        p.add(lab);
        a.add(p, BorderLayout.CENTER);


    }

    public static void main(String[] args) {

        new HelloWorldGui();

    }

}

You're setting the layout on your frame, which includes a single object, which is a panel. It's the panel you need to set the layout of, because it's the one that contains your label and button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.