我有一个使用空布局的主面板,我想在其中添加三个面板,但我希望这三个面板在加载主面板时自动调整大小。我无法使用布局管理器,因为我将拖动这三个面板。
我尝试了这段代码,但它不起作用
pnlGrid.setBounds(514,11, pnlMain.this.getWidth()/3, pnlMain.this.getHeight());
我也尝试过
pnlGrid.setPreferredSize(new Dimension(pnlMain.this.getWidth()/3, pnlMain.this.getHeight()));
但还是不好。请帮忙。谢谢!
最佳答案
使用MigLayout并在拖动每个面板时更新 ComponentRestraints 。这样您就可以准确控制它们的位置和边界。
例如:
MigLayout layout;
public Constructor(){
layout = new MigLayout();
...
container.setLayout(layout);
}
public void onMouseDrag(JPanel panel, int newX, int newY){
layout.setComponentConstraints(panel, "pos " + newX + " " + newY + ", w 100%/3, h 100%");
container.validate(); // probably not necessary
}
关于java - 如何设置空布局面板内面板的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12123474/