【发布时间】:2022-11-10 03:35:14
【问题描述】:
所以基本上我的gui是这样的:
包含名为 JPanel 的免费设计布局的框架主面板
mainPanel 内部还有另外两个面板:
一个)工具面板布局 BoxLayout
二)游戏面板布局 GridLayout
这是代码的重要位:
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);
}
问题是当我增加列数时,游戏面板或者主面板增加,但框架的宽度不增加。
例子:
如何更改框架的大小以根据面板大小调整自身大小?
笔记: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);
但它没有工作,因为框架没有调整大小!
【问题讨论】:
-
包含自由设计布局 JPanel 的框架- 没有必要。可能使用无效的空布局。默认情况下,框架使用 BorderLayout。因此,您只需使用 BorderLayout.PAGE_START 将“toolPanel”添加到框架,使用 BorderLayout.CENTER 将“gamePanel”添加到框架中。然后在使其可见之前
pack()框架。您是否解决了您的其他问题:stackoverflow.com/q/74246638/131872?看起来像,因为我看到了炸弹的图像。那么,为什么您没有对表明问题已解决的问题发表评论呢? -
我首先关心的是
initComponents在做什么。您应该避免直接从像JFrame这样的顶级容器扩展,这可能会导致“其他”问题 -
你为什么删除你的最后一个问题?您仍然没有回复说明这些建议是否有帮助。见:What should I do when someone answers my question
标签: java swing jframe jpanel awt