문제

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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top