我的按鈕排列不正確...什麼是錯的?按鈕排列不正確
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();
}
}
你在找什麼樣的禮物。有全部在水平對齊 – AwokeKnowing
我正在排列他們,就像你將一個盒子的網格,除了這些改變的大小和數量,所以佈局不能修復。 – visc
@ user2925592,您應該使用'revalidate()'not validate(),並且您只需在將組件添加到面板後執行一次即可。所以revalidate()應該在repaint()之前。 – camickr