2013-10-28 140 views
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

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

你在找什麼樣的禮物。有全部在水平對齊 – AwokeKnowing

+0

我正在排列他們,就像你將一個盒子的網格,除了這些改變的大小和數量,所以佈局不能修復。 – visc

+0

@ user2925592,您應該使用'revalidate()'not validate(),並且您只需在將組件添加到面板後執行一次即可。所以revalidate()應該在repaint()之前。 – camickr

回答

0

什麼做.getcolumns和GetRows的輸出。嘗試一下。輸出內部循環內的列和行,並確保至少得到這些數字是正確的。你確定行增量嗎?

+0

這不是那種問題。這樣可行... – visc