如何将填充添加到jtextfield [英] How can I add padding to a jtextfield

查看:75
本文介绍了如何将填充添加到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.

推荐答案

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

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().

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

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)));

这篇关于如何将填充添加到jtextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆