如何向 jtextfield 添加填充
如何向 jtextfield 添加一些填充? 我尝试过 tf.setMargin(new Insets(5,5,5,5)); ,但没有任何效果。
How can I add some padding to a jtextfield?
I've tried tf.setMargin(new Insets(5,5,5,5));
which doesn't have any effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您遇到的问题是 UI 在文本字段上设置自己的边框,覆盖您设置的边距。您可以在
setMargin()
的 javadoc 中看到对此效果的警告。解决方案是让 UI 设置一个边框,然后挤入您自己的另一个边框:
The problem you are having is that the UI is setting its own border on the text field, overriding the margin you set. You can see a warning to this effect in the javadoc of
setMargin()
.The solution is to let the UI set a border, then squeeze in another border of your own:
您查看CompoundBorder,您可以在那里设置
LineBorder(Color.gray, 1)
并与you have look at CompoundBorder, there you can set
LineBorder(Color.gray, 1)
and with最简单的方法是使用
BorderFactory
The simplest way is using
BorderFactory
我知道这已经晚了很多年,但实际上,如果您希望 Java 具有一致的外观和感觉,您应该编辑 UI,这样您创建的每个文本字段都不需要自己的特殊代码。因此,以 Russel Zahniser 上面的例子为例:
I know this is years too late, but actually, if you want a consistent look and feel in Java, you should be editing the UI so every text field you create doesn't need its own special code. So, taking from Russel Zahniser's example above:
这是 100% 工作
This is working 100%
您可以将其应用到已经创建的带边框的文本框
You can apply this to a textbox already created with a border