Results 1 to 1 of 1
- 02-01-2014, 10:32 PM #1Member
- Join Date
- Jan 2014
- Rep Power
How to keep constant propotion using GridBagLayout
I am working with GridBagLayout. Now I have encountered a problem as follows.
I have a JPanel A.
On this JPanel A , I put two other JPanels,say JPanel B and JPanel C. One take the left side of A, one take the right side of A.
The propotion of width between B and C changes depending on what components in in B and C(precisely speaking, depends on the size of the components in B and C).
My task is to keep the propotion of width between B and C constant, say 2:1.
demo code illustrates the problem:
JFrame frame=new JFrame(); frame.setSize(500,500); JPanel A=new JPanel(new GridBagLayout()); frame.setContentPane(A); JPanel B=new JPanel(new GridBagLayout()); B.setBackground(Color.gray); GridBagConstraints c=new GridBagConstraints(); c.gridx=0; c.gridy=0; c.weightx=0.6; c.weighty=1; c.fill=GridBagConstraints.BOTH; A.add(B,c); JPanel C=new JPanel(new GridBagLayout()); C.setBackground(Color.blue); JButton bt=new JButton(); //bt.setPreferredSize(new Dimension(200, 40)); A.add(bt); c.gridx=1; c.gridy=0; c.weightx=0.4; c.fill=GridBagConstraints.BOTH; A.add(C,c); frame.setVisible(true);
Last edited by tianchen; 02-01-2014 at 10:34 PM.
- By dojob in forum New To JavaReplies: 6Last Post: 09-17-2013, 08:02 PM
- By sonikku in forum New To JavaReplies: 5Last Post: 05-17-2013, 08:41 AM
- By KiranA in forum EclipseReplies: 1Last Post: 12-01-2011, 09:19 AM
- By harshakantha in forum New To JavaReplies: 3Last Post: 06-13-2011, 07:51 AM
- By itaipee in forum AWT / SwingReplies: 1Last Post: 12-30-2008, 04:12 PM