简体   繁体   中英

JTextField size

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.weightx = 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?

Try to set constPanelInfo.iPadX=100; instead of setting preferred size.

Hopefully this is what you're looking for... setting

constPanelInfo.weightx = 0;

for the sensor should keep it at the specified 100px. ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM