0

我的按钮排列不正确……怎么了?

private void loadPuzzleButtons()
{
    if (active_puzzle != null)
    {
        int devider = 5;
        int count = 0;
        JToggleButton puzzleButton[] = new JToggleButton[active_puzzle.getNumberOfPieces()];
        for(int row = 0; row < active_puzzle.getRows(); row++)
        {
            for(int column = 0; column < active_puzzle.getColumns(); column++)
            {
                puzzleButton[count] = new JToggleButton(new ImageIcon( active_puzzle.getPieces()[count].getPieceImage() ) );
                puzzleButton[count].setLocation(200 + active_puzzle.getPieceWidth() * column + devider * column,
                                                200 + active_puzzle.getPieceHeight() * row + devider * row);
                puzzleButton[count].setContentAreaFilled(false);
                puzzleButton[count].setBorderPainted(false);
                puzzleButton[count].setBorder(null);
                mainPuzzlerPanel.add(puzzleButton[count]);
                mainPuzzlerPanel.validate();
                count++;
            }
        }
        mainPuzzlerPanel.repaint();
    }
}

这是输出的照片:http: //i.imgur.com/Zdink2Q.png

在此处输入图像描述

对不起,我会给你我的整个代码,但它更大而且我不会这样做......

提前谢谢您,如果您需要更多信息,请询问!

我想给那些不想看到我如何解决问题的人。

private void loadPuzzleButtons()
{
    if (active_puzzle != null)
    {
        int count = 0;
        GridLayout puzzleLayout = new GridLayout(active_puzzle.getRows(),active_puzzle.getColumns(),3,3);
        puzzlePanel.setLayout(puzzleLayout);
        JToggleButton puzzleButton[] = new JToggleButton[active_puzzle.getNumberOfPieces()];
        for(int row = 0; row < active_puzzle.getRows(); row++)
        {
            for(int column = 0; column < active_puzzle.getColumns(); column++)
            {
                puzzleButton[count] = new JToggleButton(new ImageIcon(active_puzzle.getPieces()[count].getPieceImage()));
                puzzleButton[count].setContentAreaFilled(false);
                puzzleButton[count].setBorderPainted(false);
                puzzleButton[count].setBorder(null);
                puzzlePanel.add(puzzleButton[count]);
                puzzlePanel.validate();
                count++;
            }
        }
        puzzlePanel.repaint();
    }
}
4

2 回答 2

1

尝试使用LayoutManager正确填充面板

于 2013-10-28T17:02:09.840 回答
0

.getcolumns 和 getrows 输出什么。试试看。输出内部循环内的列和行,并确保至少获得正确的数字。你确定行会增加吗?

于 2013-10-28T17:18:36.130 回答