如何将JTextFields保存在Java Swing BoxLayout中? [英] How do I keep JTextFields in a Java Swing BoxLayout from expanding?

查看:110
本文介绍了如何将JTextFields保存在Java Swing BoxLayout中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JPanel 看起来像这样:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

...

panel.add(jTextField1);
panel.add(Box.createVerticalStrut(10));
panel.add(jButton1);

panel.add(Box.createVerticalStrut(30));

panel.add(jTextField2);
panel.add(Box.createVerticalStrut(10));
panel.add(jButton2);

... //etc.

我的问题是 JTextField 成为巨大的垂直。我希望它们只对单行足够高,因为这是用户可以输入的全部内容。按钮很好(它们不会垂直扩展)。

My problem is that the JTextFields become huge vertically. I want them to only be high enough for a single line, since that is all that the user can type in them. The buttons are fine (they don't expand vertically).

有没有办法保持 JTextField s从扩大?我对Swing很新,所以让我知道我是否做了一切可怕的错误。

Is there any way to keep the JTextFields from expanding? I'm pretty new to Swing, so let me know if I'm doing everything horribly wrong.

推荐答案

textField = new JTextField( ... );
textField.setMaximumSize( textField.getPreferredSize() );

这篇关于如何将JTextFields保存在Java Swing BoxLayout中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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