为什么在设置新的Border后我的JTextField发生了变化 [英] Why my JTextField has changed after set new Border
问题描述
我想知道为什么设置新的Border后,我的元素JTextField会改变高度:
I would like to know why my element JTextField has change height after setting a new Border:
packagesAddField.setBorder(BorderFactory.createLineBorder(Color.RED));
在此代码之后,我的JTextField更改了高度(距上端约1px,距页脚约1px).有可能,当我设置一些边框时,他可以更改组件的外观吗?
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?
推荐答案
总之,不要. setBorder()
API建议您将边框添加到封闭容器中
In a word, don't. The setBorder()
API recommends that you add the border to the enclosing container:
尽管从技术上讲,您可以在继承自
JComponent
的任何对象上设置边框,但是许多标准Swing组件的外观实现都无法与用户设置的边框很好地配合.通常,当您要在JPanel
或JLabel
以外的标准Swing组件上设置边框时,建议您将组件放在JPanel
中,并在JPanel
上设置边框.
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
.
或者,考虑使用 InputVerifier
.
Alternatively, consider an InputVerifier
.
这篇关于为什么在设置新的Border后我的JTextField发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!