如何向 jtextfield 添加填充

发布于 2024-12-26 01:59:33 字数 79 浏览 3 评论 0原文

如何向 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 技术交流群。

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

发布评论

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

评论(6

深海少女心 2025-01-02 01:59:33

您遇到的问题是 UI 在文本字段上设置自己的边框,覆盖您设置的边距。您可以在 setMargin() 的 javadoc 中看到对此效果的警告。

解决方案是让 UI 设置一个边框,然后挤入您自己的另一个边框:

field.setBorder(BorderFactory.createCompoundBorder(
        field.getBorder(), 
        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

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:

field.setBorder(BorderFactory.createCompoundBorder(
        field.getBorder(), 
        BorderFactory.createEmptyBorder(5, 5, 5, 5)));
懒的傷心 2025-01-02 01:59:33

您查看CompoundBorder,您可以在那里设置LineBorder(Color.gray, 1) 并与

EmptyBorder(5, 5, 5, 5)

you have look at CompoundBorder, there you can set LineBorder(Color.gray, 1) and with

EmptyBorder(5, 5, 5, 5)
万劫不复 2025-01-02 01:59:33

最简单的方法是使用 BorderFactory

field.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

The simplest way is using BorderFactory

field.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
说不完的你爱 2025-01-02 01:59:33

我知道这已经晚了很多年,但实际上,如果您希望 Java 具有一致的外观和感觉,您应该编辑 UI,这样您创建的每个文本字段都不需要自己的特殊代码。因此,以 Russel Zahniser 上面的例子为例:

Border tfBorder = UIManager.getBorder("TextField.border");
Border newBorder = BorderFactory.createCompoundBorder(tfBorder, 
        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

UIManager.setBorder("TextField.border", newBorder);

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:

Border tfBorder = UIManager.getBorder("TextField.border");
Border newBorder = BorderFactory.createCompoundBorder(tfBorder, 
        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

UIManager.setBorder("TextField.border", newBorder);
魔法唧唧 2025-01-02 01:59:33
yourTextFeildVariable.setBorder(BorderFactory.createCompoundBorder(yourTextFeildVariable.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));

这是 100% 工作

在此处输入图像描述

yourTextFeildVariable.setBorder(BorderFactory.createCompoundBorder(yourTextFeildVariable.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));

This is working 100%

enter image description here

谁把谁当真 2025-01-02 01:59:33

您可以将其应用到已经创建的带边框的文本框

jTextField1.setBorder(BorderFactory.createCompoundBorder(jTextField1.getBorder(), BorderFactory.createEmptyBorder(6, 6, 6, 6)));

You can apply this to a textbox already created with a border

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