2011-01-12 7 views
0

GridLayout이있는 패널이 있습니다.java, swing, Gridlayout 문제

하지만 프로그램을 실행하려고하면 첫 번째 버튼 만 100 개가 표시됩니다. 또한 나머지는 커서를 위로 움직일 때만 나타납니다. 무엇이 문제입니까?

가 여기에 전체 클래스의

public class MainLayout extends JFrame { 

    public MainLayout() { 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(650, 750); 
     setLayout(new FlowLayout()); 
     //setResizable(false); 

     final JPanel gridPanel = new JPanel(new GridLayout(Life.CELLS, Life.CELLS)); 

     for (int i=0; i<Life.CELLS; i++) { 
      for (int j=0; j<Life.CELLS; j++) { 
       CellButton jb = new CellButton(i, j); 
       jb.setPreferredSize(new Dimension(jb.getIcon().getIconHeight(), jb.getIcon().getIconWidth())); 
       buttons[i][j] = jb; 
       grid[i][j] = false; 

       gridPanel.add(jb); 
      } 
     } 
     add(gridPanel); 
    } 
} 

이 CellButton

의 코드는 (CellButton가하는 JButton를 확장하는 클래스입니다 Life.CELLS = 10)
package classes; 

import javax.swing.JButton; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 

public class CellButton extends JButton { 

    private int x; 
    private int y; 
    boolean alive; 
    ImageIcon icon; 
    boolean next; 

    // icons for grids 
    final ImageIcon dead = 
     new ImageIcon(JFrame.class.getResource("/images/image1.gif")); 
    final ImageIcon live = 
     new ImageIcon(JFrame.class.getResource("/images/image2.gif")); 

    public CellButton(int X, int Y) { 
     super(); 
     x = X; 
     y = Y; 
     alive = false; 
     icon = dead; 
     setIcon(icon); 
    } 

    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    public boolean isAlive() { 
     return alive; 
    } 

    public void relive() { 
     alive = true; 
     icon = live; 
     setIcon(icon); 
    } 

    public void die() { 
     alive = false; 
     icon = dead; 
     setIcon(icon); 
    } 

    public void setNext(boolean n) { 
     next = n; 
    } 

    public boolean getNext() { 
     return next; 
    } 

    public ImageIcon getIcon() { 
     return icon; 
    } 
} 
+0

'CellButton' 코드를 게시하십시오. – Qwerky

+0

CellButton이 추가되었습니다. – josh

+0

내 대답이 업데이트되었습니다. x와 y를 제거해야합니다. JComponent의 레이아웃 계산을 깨뜨리고 있습니다. – Qwerky

답변

0

사물의 숫자는 할 수있다 틀리다; 그러나 더 많은 코드가 없으면 알기가 어렵습니다. 참고로, 다음은 작동하는 example입니다.

0

코드에서 다른 작업을 수행해야합니다. 사용중인 정확한 코드를 게시하십시오. 다음 테스트에서는 예상대로 JButtons의 10x10 표를 보여줍니다.

import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class GridLayoutTest 
{ 
public static void main(String[] args) 
{ 
    JFrame frame = new JFrame(); 
    JPanel panel = new JPanel(new GridLayout(10,10,10,10)); 

    for (int i=0; i<100; i++) 
    { 
    panel.add(new JButton(String.valueOf(i))); 
    } 

    frame.add(panel); 

    frame.setSize(600,600); 
    frame.setVisible(true); 
} 
} 

편집 : 나는 당신의 문제를 볼
, 당신은 잘못 JComponent에서 getX()getY() 방법을 무시했다. int xint y 변수를 제거하고 getX()getY() 메소드 JButton을 이미 제공합니다.

+0

이 코드와 본질적인 차이점은 없습니다. – josh