所以基本上我的gui是這樣的:
包含名為mainPanel的免費設計布局 JPanel 的框架
mainPanel 內部還有另外兩個面板:
A)布局BoxLayout的toolPanel
B)布局GridLayout的gamePanel
這是代碼的重要位:
public Normalv3() {
initComponents();
importBombs();
}
public void importBombs(){
rows = Minesweeper.rows;
columns = Minesweeper.columns;
total = rows*columns;
bombsIndexes = new ArrayList<Integer>(Minesweeper.totalBombs);
gamePanel.setLayout(new java.awt.GridLayout(rows,columns)); //Creating a new grid layout and putting it inside the old layout
////
Random ran = new Random();
int low=1;
int high=rows*columns;
int ranValue;
for (int i = 0 ; i< Minesweeper.totalBombs;i ){
ranValue = ran.nextInt(high - low) low;
if(bombsIndexes.contains(ranValue)){
i--;
continue;
}
bombsIndexes.add(ranValue);
}
////
for (int i = 1; i <= total ; i ){
btnMines b = new btnMines(i);
if(bombsIndexes.contains(i)){
b.isBomb = true;
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/minesweeper/resources/bomb.png")));
}
b.setPreferredSize(new Dimension(20,20));
gamePanel.add(b);
}
this.setResizable(false);
this.setTitle("Minesweeper");
this.pack();//sizes frame so that all components are at their preferred sizes
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.validate(); //recalculate layout
this.setVisible(true);
}
問題是當我增加列數時,gamePanel或mainPanel的寬度會增加,但框架的寬度不會。
例子:

如何更改框架的大小以根據面板大小調整自身大小?
注意: btnMines 基本上只是一個帶有一些變數的 JButton。
額外的問題:我將如何將每個按鈕變成正方形?如您所見,我嘗試通過撰寫 b.setPreferredSize(new Dimension(20,20)); 但每個生成的按鈕仍然是矩形!
請幫忙!
另外,我知道代碼很亂。我曾經將該函式分離為單獨的函式,但現在我決定將其全部放入 1 個函式中,以便正確測驗它。
謝謝!
我嘗試添加:
this.setResizable(false);
this.setTitle("Minesweeper");
this.pack();//sizes frame so that all components are at their preferred sizes
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.validate(); //recalculate layout
this.setVisible(true);
但它沒有作業,因為框架沒有調整大小!
uj5u.com熱心網友回復:
包含名為 mainPanel 的免費設計布局 JPanel 的框架
這似乎表明mainPanel可能正在使用null布局,這會導致您出現問題,但這只是一個猜測。
JFrame#pack應該根據通過后續布局管理器提供的內容“首選大小”提示將框架包裹在內容周圍,這讓我質疑您的設計中涉及哪些布局管理器(如果有的話)。
例如...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new Normalv3(10, 10));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Normalv3 extends JPanel {
public Normalv3(int cols, int rows) {
setLayout(new BorderLayout());
add(makeControlsPane(), BorderLayout.NORTH);
add(makeGamePane(cols, rows));
}
protected JPanel makeControlsPane() {
JPanel pane = new JPanel(new FlowLayout(FlowLayout.LEADING));
pane.add(new JButton("Back to Menu"));
pane.add(new JButton("Reset"));
return pane;
}
protected JPanel makeGamePane(int cols, int rows) {
JPanel pane = new JPanel(new GridLayout(rows, cols));
for (int index = 0; index < (cols * rows); index ) {
JButton btn = new SquareButton("*");
pane.add(btn);
}
return pane;
}
}
class SquareButton extends JButton {
SquareButton(String s) {
super(s);
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int s = (int) (d.getWidth() < d.getHeight() ? d.getHeight() : d.getWidth());
return new Dimension(s, s);
}
}
}
方形按鈕設計取自在 Swing 中創建方形按鈕
作為旁注 - 我鼓勵您注意使用表單設計器,它們往往會很快變得混亂并且難以診斷和維護(恕我直言)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529935.html
標籤:爪哇摇摆框架面板awt
