JTextField 大小

发布于 2024-11-16 17:56:49 字数 885 浏览 3 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-11-23 17:56:49

希望这就是您正在寻找的...

constPanelInfo.weightx = 0;

传感器设置应将其保持在指定的 100 像素。即,如果所有权重都为零,则所有额外空间都出现在单元格的网格和左右边缘之间。

请记住,如果设置了权重,那么

constPanelInfo.fill = GridBagConstraints.HORIZONTAL;

无论首选大小如何,campoSensor 都会填充单元格的整个宽度。
该宽度可能由面板所在的 JFrame 的大小决定。

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.

晨曦÷微暖 2024-11-23 17:56:49

尝试设置 constPanelInfo.iPadX=100; 而不是设置首选大小。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文