javaeclipseswinglayout-manager

Button Takes Up My Whole Screen


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class box{
    public static String b1state = "A";
    public static String b2state = "S";
    public static String b3state = "D";
    public static String b4state = "F";
    public static String b5state = "G";
    public static String b6state = "H";
    public static String b7state = "J";
    public static String b8state = "K";
    public static String b9state = "Q";

    public static void main(String[] args){
        JFrame frame = new JFrame("Tic Tac Toe");
        frame.setSize(500,500);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel();

        JButton b1 = new JButton(b1state);
        b1.setLocation(100,  50);
        JButton b2 = new JButton(b2state);
        b2.setLocation(150, 50);
        JButton b3 = new JButton(b3state);
        b3.setLocation(200, 50);
        JButton b4 = new JButton(b4state);
        b4.setLocation(100, 100);
        JButton b5 = new JButton(b5state);
        b5.setLocation(150, 100);
        JButton b6 = new JButton(b6state);
        b6.setLocation(200, 100);
        JButton b7 = new JButton(b7state);
        b7.setLocation(100, 150);
        JButton b8 = new JButton(b8state);
        b8.setLocation(150, 150);
        JButton b9 = new JButton(b9state);
        b9.setLocation(200, 150);

        b1.setSize(50, 50);
        b2.setSize(50, 50);
        b3.setSize(50, 50);
        b4.setSize(50, 50);
        b5.setSize(50, 50);
        b6.setSize(50, 50);
        b7.setSize(50, 50);
        b8.setSize(50, 50);
        b9.setSize(50, 50);

        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);
        p.add(b5);
        p.add(b6);
        p.add(b7);
        p.add(b8);
        p.add(b9);

        frame.add(p);
        frame.add(b1);
        frame.add(b2);
        frame.add(b3);
        frame.add(b4);
        frame.add(b5);
        frame.add(b6);
        frame.add(b7);
        frame.add(b8);
        frame.add(b9);
    }

When ever I run this sometimes it works but other times it just opens the window with a whole button taking up the screen! Please Help!


Solution

    1. A component may only reside within a single parent, adding a component to another container will remove it from the first, so basically, nothing resides within your JPanel anymore...
    2. JFrame by default, uses a BorderLayout, which means that only one component can occupy any of the five available spaces it provides. What's happening with your code is b9, been the last component added to your frame, is the only component been displayed. BorderLayout is providing b9 with the full available space to it.

    Start by taking a look at Laying Out Components Within a Container. You may need to use multiple layouts in order to achieve what you want, but this allows you to separate responsibility and isolate the individual requirements of each section of your interface