1

现在我正在尝试使gui上的connect 4网格始终为7x8,无论窗口大小如何。我一直在尝试用 a 设置按钮数组,setMaximumSize但它不起作用。

这是设置JButton数组的代码

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
        }

以防万一我还会在此处包含窗口创建方法。

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



        }
4

2 回答 2

1

不确定您要使用 setMaximumSize 实现什么。没有明确和精确的要求,我们几乎无法为您提供帮助。

因此,我建议您查看以下代码段(这是一个SSCCE)并尝试找出您做错了什么:

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

}
于 2012-10-24T18:02:02.923 回答
0

setMaximumSize()限制某物的大小。根据您使用的布局管理器,您需要setPreferredSize()setSize().

于 2012-10-24T17:22:31.030 回答