1

Right now I am trying to make it so that the connect 4 grid on the gui is always a 7x8 no matter what window size. I have been trying to set the button array with a setMaximumSize and it's not working.

Here is the code that sets the JButton array

void ResetGame()
        {

            JLabel label = new JLabel("Click a column to drop piece");



            for(int r=0;r<gameBoard.length;r++)
            {
                java.util.Arrays.fill(gameBoard[r],0,gameBoard[r].length,'0');



                //loop through board columns
                for(int c=0;c<gameBoard[r].length;c++)
                {
                    gameButtons[r][c]= new JButton(empty);
                    panel.add(gameButtons[r][c]);
                    gameButtons[r][c].setPreferredSize(new Dimension(70,70));
                    //Allows buttons to be arranged as grid.
                    GridLayout grid = new GridLayout(0,8);
                    //Sets into grid.
                    gameButtons[r][c].setLayout(grid);
                    gameButtons[r][c].setMaximumSize(new Dimension(0,10));

                }
                panel.add(label); 



            }
            // loop through array setting char array back to ' ' and buttons array back to empty pic
            // reset currentPlayer and numMoves variables
        }

Just in case I'll also include the window creation method here.

public void CreateWindow()
        {
            //Sets window title and create window object.
            JFrame aWindow = new JFrame("Connect Four");
            //Set window position and size
            aWindow.setBounds(200,100,600,800);
            //What close button does.
            aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Make window visible.
            aWindow.setVisible(true);
            //Sets content area to work with stuff.
            aWindow.setContentPane(panel);
             //Gets content pane.
            Container content = aWindow.getContentPane();



        }
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 2
    To get the best answers for a question like this, it's usually best for you to create and post an [sscce](http://sscce.org), code that we can run and modify that requires no outside resources (except perhaps images readily available online). – Hovercraft Full Of Eels Oct 24 '12 at 17:25
  • 1
    As HFOE said, post an SSCCE, but meanwhile, this line seems quite strange: `gameButtons[r][c].setLayout(grid);` Are you adding components to your buttons (this is valid, but I don't see it very often)? Don't you rather want the layout manager to be set on your `panel`? – Guillaume Polet Oct 24 '12 at 17:31
  • 1
    `GridLayout` does not respect a component's maximum size. And what do you mean by not working? – David Kroukamp Oct 24 '12 at 17:35
  • I made it as short as possible, I only included the window method and the board reset method. – DrinkJavaCodeJava Oct 24 '12 at 17:50
  • I was just testing out the function by putting extreme values to create dramatic changes. Thats not what I am seriously going to use. – DrinkJavaCodeJava Oct 24 '12 at 18:05

2 Answers2

1

Not sure of what you are trying to achieve with setMaximumSize. Without explicit and precise requirements, we can hardly help you.

So, I would suggest that you take a look at the following snippet (which is an SSCCE) and try to find out what you are doing wrong:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Connect4 {

    public class GameButton extends JPanel {
        private final int row;
        private final int column;
        private Color color;

        public GameButton(final int row, final int column) {
            super();
            this.row = row;
            this.column = column;
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    System.out.println("Game button " + row + " " + column + " has been pressed");
                }
            });
        }

        public void setColor(Color color) {
            this.color = color;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int size = Math.min(getWidth(), getHeight());
            int offset = (int) ((double) size / 10);
            size = size - 2 * offset;
            if (color == null) {
                g.setColor(Color.BLACK);
                g.drawOval(offset, offset, size, size);
            } else {
                g.setColor(color);
                g.fillOval(offset, offset, size, size);
            }
        }
    }

    protected void initUI() {
        JPanel gridPanel = new JPanel(new GridLayout(7, 8));
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 8; j++) {
                GameButton gameButton = new GameButton(i, j);
                gridPanel.add(gameButton);
            }
        }
        JFrame frame = new JFrame(Connect4.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(gridPanel);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Connect4().initUI();
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

setMaximumSize() puts a bound on how large something can be. Depending on what you are using for a layout manager you want either setPreferredSize() or setSize().

thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
  • no, neither of those, see f.i. http://stackoverflow.com/q/7229226/203657 for the whys. The answer to layout problems _always_ is a suitable LayoutManager – kleopatra Oct 24 '12 at 18:17