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;
}
}
'CellButton' 코드를 게시하십시오. – Qwerky
CellButton이 추가되었습니다. – josh
내 대답이 업데이트되었습니다. x와 y를 제거해야합니다. JComponent의 레이아웃 계산을 깨뜨리고 있습니다. – Qwerky