我有一个这样的JPanel:
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);其中,campoSensor定义为:
private JTextField campoSensor = new JTextField();...but JTextField并没有像我想要的那样缩放到100px的重量:它保持了在编写setPreferredSize方法之前的大小。有什么想法吗?
发布于 2011-06-22 18:30:02
希望这就是你要找的..。设置
constPanelInfo.weightx = 0;传感器应将其保持在指定的100px。也就是说,如果所有的权重都为零,那么所有额外的空间都会出现在单元格的网格和左右边缘之间。
请记住,如果设置了重量,并且由于
constPanelInfo.fill = GridBagConstraints.HORIZONTAL;campoSensor将填充单元格的整个宽度,而不考虑首选大小。此宽度可能由您的面板所在的JFrame的大小决定。
发布于 2011-06-22 17:59:12
尝试设置constPanelInfo.iPadX=100;,而不是设置首选大小。
https://stackoverflow.com/questions/6437968
复制相似问题