3
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!

5
  • 5
    Learn about how the Swing layout managers work and you'll see why your button fills the screen. To get the link, hover over the swing tag I've added to your question and click on "info" and then the layout manager tutorial. Note that JFrame contentPanes use BorderLayout. You may want to read on GridLayout, and you may wish to get rid of those useless calls to setLocation(...). They're not going to help you, and right now are only fooling you into thinking that they're doing something when they're not, and are not even needed. Sep 5, 2014 at 1:42
  • 1
    Your link is here. Sep 5, 2014 at 1:44
  • Other suggestions going forward -- don't setSize or setBounds either. Let the components and the layout managers set the sizes. Avoid putting the whole GUI together in the static main method. That method should be just for creating an object and starting it up, and that's about it, and this should take just a few lines of code. Also you'll want to learn and follow Java naming rules, including giving classes names that begin with a capital letter. This way others will more easily understand your code. Sep 5, 2014 at 1:50
  • BTW - the code example probably would have failed with just 2 buttons, so there's no need for the other seven. Sep 5, 2014 at 3:37
  • You have taken a JPanel p in this case, and added all JButtons to it, now it's time to add this JPanel to some container. But you starting adding those JButtons again to some other place. Just try this, take a JPanel. Change it's layout to GridLayout by p.setLayout(new GridLayout(3, 3, 5, 5)), now add all JButtons to it, like p.add(b1) and so on. Now add this JPanel to JFrame by frame.setContentPane(p), now call pack(), and then call frame.setVisible(true). Do not call setVisible() before adding needed components to the container.
    – nIcE cOw
    Sep 5, 2014 at 8:21

4 Answers 4

3
  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

1

Replace this:

    JPanel p = new JPanel();

with:

    JPanel p = new JPanel(new GridLayout(3, 3));

And this:

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

with this:

    frame.setContentPane(p);

This will make your code "work". But you have to play more with the containers to learn how to "layout" components. I suggest you this: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

0
1

i had a problem just like that if its the last button that fill your window you can "cheat" by adding another button at the end of your code and then disable him and make him invisible like this. cheater.setVisible(false); cheater.setEnabled(false);

-1

you can also fix this by setting your layout to null so panel.setLayout(null);

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 9, 2023 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.