Reputation: 169
I'm using a gridbaglayout for a Java project of mine and for some reason when I launch the program all the JLabels seem to be really small even though I've sized them using percentages(thus the sizing should be relative)
Here is the code I used to set the preferred size:
int stswidth = (int)(((float)66/100) * getWidth()), stsheight = (int)(((float)65/100) * getHeight());
int navwidth = (int)(((float)21.5f/100) * getWidth()), navheight = (int)(((float)10/100) * getHeight());
int clwidth = (int)(((float)25/100) * getWidth()), clheight = (int)(((float)80/100) * getHeight());
int hwidth = (int)(((float)100/100) * getWidth()), hheight = (int)(((float)20/100) * getHeight());
Here is the code to set the position of each label using grid bag constraints:
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
core.add(sts[1], c);
//nav
c.gridx = 0;
c.gridy = 0;
nav.add(navb[0], c);
c.gridx = 1;
c.gridy = 0;
nav.add(navb[1], c);
c.gridx = 2;
c.gridy = 0;
nav.add(navb[2], c);
//cl
c.gridx = 0;
c.gridy = 0;
clock.add(cl, c);
head.add(header, c);
Upvotes: 2
Views: 70
Reputation: 285405
I don't see where you're setting any of the constraints weightx and weighty. Try giving them default values of 1.0.
c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
This will help prevent all the components from scrunching up in the center.
And yeah, if this doesn't help, then create and post your sscce.
Upvotes: 4