为什么在设置新的Border后我的JTextField发生了变化 [英] Why my JTextField has changed after set new Border

查看:110
本文介绍了为什么在设置新的Border后我的JTextField发生了变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么设置新的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组件的外观实现都无法与用户设置的边框很好地配合.通常,当您要在JPanelJLabel以外的标准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 than JPanel or JLabel, we recommend that you put the component in a JPanel and set the border on the JPanel.

或者,考虑使用 InputVerifier .

Alternatively, consider an InputVerifier.

这篇关于为什么在设置新的Border后我的JTextField发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆