Si potrebbe anche provare a utilizzare un EmptyBorder per inserire spazi tra i due componenti. Se hai già un bordo, puoi usarlo insieme a un EmptyBorder per creare un CompoundBorder. Nel frammento di codice sottostante, viene creato un nuovo CompoundBorder con un TitledBorder e un EmptyBorder che applica una spaziatura di 1 pixel attorno al componente.
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)
)
);