How can I add padding to a jtextfield

后端 未结 6 1735
予麋鹿
予麋鹿 2021-01-01 12:08

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.

6条回答
  •  感动是毒
    2021-01-01 12:35

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

提交回复
热议问题