【发布时间】:2023-03-11 06:44:01
【问题描述】:
我正在用 Java 克隆 Minesweeper。一切都很好,但我想在顶部添加一个柜台。现在,一些按钮不在屏幕上。我必须更改哪些布局才能修复它?
在添加计数器之前: (https://i.imgur.com/cSKmCGr.jpg)
添加“计数器”后 (https://i.imgur.com/fbMQqQz.jpg)
这是我的代码:
public static JPanel generate() {
i2w = row*row-ilosc_min;
przyc = new Sap[row][row];
JPanel all = new JPanel(); //panel that contains counter and buttons
JPanel licznik = new JPanel(); //Counter
licznik.setBackground(Color.red);
licznik.setPreferredSize(new Dimension(MyFrame.wym-50, 50)); //counter takes 50 px height and full width of frame
all.add(licznik);
JPanel panel = new JPanel(); //Buttons panel
panel.setVisible(true);
panel.setLayout(new GridLayout(row, row));
for(int x = 0; x < row; x++) { //creating buttons
for(int y = 0; y < row; y++) {
Sap sp = new Sap(x, y); //My buttons that extends JButton
przyc[x][y] = sp;
panel.add(sp); //Adding button to panel
}
}
all.add(panel); //All contains counter and buttons
return all; //Returning it
}
当我不创建计数器面板时,按钮就像第二张照片中一样。
抱歉有任何错误,英语不是我的主要语言。
【问题讨论】:
-
调用 Panel 所在 JFrame 的 pack()。
-
link 现在更糟了。
all.add(panel); Main.getFrame().pack(); return all;
标签: java image button layout grid-layout