I have a JPanel like this:
GridBagConstraints constPanelInfo = new GridBagConstraints();
panelInfo = new JPanel(new GridBagLayout());
JLabel sensor1 = new JLabel("Sensor: ");
constPanelInfo.gridx = 0;
constPanelInfo.gridy = 0;
constPanelInfo.we开发者_如何转开发ightx = 0.0;
constPanelInfo.fill = GridBagConstraints.NONE;
panelInfo.add(sensor1, constPanelInfo);
constPanelInfo.gridx = 1;
constPanelInfo.gridy = 0;
constPanelInfo.weightx = 1.0;
constPanelInfo.fill = GridBagConstraints.HORIZONTAL;
campoSensor.setPreferredSize(new Dimension(100, campoSensor.getPreferredSize().height));
panelInfo.add(campoSensor, constPanelInfo);
where campoSensor is defined as:
private JTextField campoSensor = new JTextField();
...but the JTextField is not scaled to 100 px weight as I want: it stays the same size it had before writting the setPreferredSize method. Any idea here?
Hopefully this is what you're looking for... setting
constPanelInfo.weightx = 0;
for the sensor should keep it at the specified 100px. i.e If all the weights are zero, all the extra space appears between the grids of the cell and the left and right edges.
Remember that if the weigths are set and because of
constPanelInfo.fill = GridBagConstraints.HORIZONTAL;
The campoSensor is going to fill the full width of the cell regardless of the preferred size. This width will probably be determined by the size of the JFrame that your panel is sitting in.
Try to set constPanelInfo.iPadX=100;
instead of setting preferred size.
精彩评论