问题
I would like to know why my element JTextField has change height after setting a new Border:
packagesAddField.setBorder(BorderFactory.createLineBorder(Color.RED));
after this code my JTextField changing a height (about 1px from upper and 1px from footer). It is possible, when i set some border he can change appearance of component?
回答1:
In a word, don't. The setBorder() API recommends that you add the border to the enclosing container:
Although technically you can set the border on any object that inherits from
JComponent
, the look and feel implementation of many standard Swing components doesn't work well with user-set borders. In general, when you want to set a border on a standard Swing component other thanJPanel
orJLabel
, we recommend that you put the component in aJPanel
and set the border on theJPanel
.
Alternatively, consider an InputVerifier.
来源:https://stackoverflow.com/questions/24850292/why-my-jtextfield-has-changed-after-set-new-border