How can I add padding to a jtextfield

后端 未结 6 1736
予麋鹿
予麋鹿 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:42

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

提交回复
热议问题