JTextArea and JTextField internal padding on text

血红的双手。 提交于 2019-12-21 09:36:11

问题


I would like to increase the spacing / padding / insets for the JTextField and JTextArea. Effectively increase the spacing between the two red lines in the image below:


回答1:


You could also try using an EmptyBorder to put in gaps between the two components. If you already have a Border, you can use that in conjunction with an EmptyBorder to create a CompoundBorder. In the code fragment below, a new CompoundBorder is created that has a TitledBorder and an EmptyBorder which enforces a padding of 1 pixed around the component.

testPanel.setBorder(
   javax.swing.BorderFactory.createCompoundBorder(
      javax.swing.BorderFactory.createTitledBorder(
         null, "Border Title",
         javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
         javax.swing.border.TitledBorder.DEFAULT_POSITION,
         new java.awt.Font("Verdana", 1, 11)
      ),
      javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)
   )
);



回答2:


Have you tried the setMargin method?




回答3:


The simplest way to achieve this is:

yourVariableName.setMargin(new Insets(2,2,2,2));

The digits represent (top, left, bottom, right). Works for both JTextField and JTextArea



来源:https://stackoverflow.com/questions/2286881/jtextarea-and-jtextfield-internal-padding-on-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!