JTextField 大小
我有一个像这样的 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();
...但是 JTextField 没有按照我想要的那样缩放到 100 px 权重:它保持与编写 setPreferredSize 方法之前的大小相同。这里有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这就是您正在寻找的...
传感器设置应将其保持在指定的 100 像素。即,如果所有权重都为零,则所有额外空间都出现在单元格的网格和左右边缘之间。
请记住,如果设置了权重,那么
无论首选大小如何,campoSensor 都会填充单元格的整个宽度。
该宽度可能由面板所在的 JFrame 的大小决定。
Hopefully this is what you're looking for... setting
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
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.
尝试设置
constPanelInfo.iPadX=100;
而不是设置首选大小。Try to set
constPanelInfo.iPadX=100;
instead of setting preferred size.