开发者

setBorder on JTextField does not work ? or does it?

The code is like this:

JTextField txt = new JTextField();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));

However the text field is ignoring my call to setBorder. No changes whatsoever.

I were to replace it with a JLabel (for instance)

JLabel txt = new JLabel();
txt.setBorder(BorderFactory.createMatteBord开发者_JS百科er(2,2,2,2,Color.red));

I would see the red border.

Can anybody tell me why? Or even better explain to me how to add a border in the JTextField?


Check out this explanation/recommendation from the Java API

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.

So... you should nest your JTextField in a JPanel or JLabel, and put the border on the JPanel or JLabel. Voila!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜