gpt4 book ai didi

java - 如何设置 JTextField 的大小(以像素为单位)

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:24 26 4
gpt4 key购买 nike

嘿,我只是想知道是否有办法设置 JTextField 宽度,例如屏幕尺寸的 50%。我知道如何获取屏幕尺寸,只需 JTextField 将其设置为字符数而不是像素

textfield = new JTextField("你好", 20);

有办法做到这一点吗?

最佳答案

以像素为单位设置 JTextField 的大小会导致您使用绝对定位,这通常是不可取的。来自 Doing Without a Layout Manager

Although it is possible to do without a layout manager, you should use a layout manager if at all possible. Layout managers also can be reused easily by other containers, as well as other programs.

看看现有 layout managers 提供的功能和 avoid using the setXXXSize() component methods 。例如,GridLayout(甚至GridBagLayout)可用于将 JTextField 宽度调整为框架客户区域的 50%。

关于java - 如何设置 JTextField 的大小(以像素为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com