0

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();

    }

}
2
Contributors
1
Reply
12
Views
3 Years
Discussion Span
Last Post by scudzilla
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.