按钮没有正确排列

时间:2013-10-28 16:58:24

标签: java swing layout awt layout-manager

我的按钮排列不正确......怎么回事?

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

enter image description here

对不起,我会给你我的全部代码,但它更大,我不会那么做......

提前感谢您,请询问您是否需要更多信息!

我想到了那些不知道我是如何解决这个问题的人。

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

2 个答案:

答案 0 :(得分:1)

尝试使用LayoutManager正确填充面板

答案 1 :(得分:0)

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