当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to set the size of a panel inside a panel with null layout?

问题描述:

I have a main panel using a null layout and I want to add three more panel in it but I want those three panels to automatically resize upon loading of the main panel. I cant use layout managers since I will be dragging those three panels.

I tried this code but it doesnt work

pnlGrid.setBounds(514,11, pnlMain.this.getWidth()/3, pnlMain.this.getHeight());

I also tried

pnlGrid.setPreferredSize(new Dimension(pnlMain.this.getWidth()/3, pnlMain.this.getHeight()));

but still not good. Please help. Thanks!

网友答案:

Use MigLayout and update the ComponentRestraints when your dragging each panel. That way you can control exactly where they are and what their bounds are.

For example:

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
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: