简体   繁体   English

Java,Swing:如何设置JTextField的最大宽度?

[英]Java, Swing: how do I set the maximum width of a JTextField?

I'm writing a custom file selection component. 我正在编写自定义文件选择组件。 In my UI, first the user clicks a button, which pops a JFileChooser ; 在我的UI中,首先用户点击一个弹出JFileChooser的按钮; when it is closed, the absolute path of the selected file is written to a JTextField . 当它关闭时,所选文件的绝对路径将写入JTextField

The problem is, absolute paths are usually long, which causes the text field to enlarge, making its container too wide. 问题是,绝对路径通常很长,导致文本字段放大,使其容器太宽。

I've tried this, but it didn't do anything, the text field is still too wide: 我试过这个,但它没有做任何事情,文本字段仍然太宽:

fileNameTextField.setMaximumSize(new java.awt.Dimension(450, 2147483647));

Currently, when it is empty, it is already 400px long, because of GridBagConstraints attached to it. 目前,当它为空时,它已经是400px长,因为附加了GridBagConstraints

I'd like it to be like text fields in HTML pages, which have a fixed size and do not enlarge when the input is too long. 我希望它像HTML页面中的文本字段一样,它具有固定的大小,并且在输入太长时不会放大。

So, how do I set the max size for a JTextField ? 那么,如何设置JTextField的最大大小?

It may depend on the layout manager your text field is in. Some layout managers expand and some do not. 它可能取决于您的文本字段所在的布局管理器。某些布局管理器会扩展,而有些则不会。 Some expand only in some cases, others always. 有些只在某些情况下扩展,有些则总是扩展。

I'm assuming you're doing 我假设你在做

filedNameTextField = new JTextField(80); // 80 == columns

If so, for most reasonable layouts, the field should not change size (at least, it shouldn't grow). 如果是这样,对于大多数合理的布局,字段不应该改变大小(至少,它不应该增长)。 Often layout managers behave badly when put into JScrollPane s. 布局管理器在放入JScrollPane时经常表现不佳。

In my experience, trying to control the sizes via setMaximumSize and setPreferredWidth and so on are precarious at best. 根据我的经验,尝试通过setMaximumSizesetPreferredWidth控制大小等等充其量是不稳定的。 Swing decided on its own with the layout manager and there's little you can do about it. Swing自己决定使用布局管理器,你几乎无能为力。

All that being said, I have no had the problem you are experiencing, which leads me to believe that some judicious use of a layout manager will solve the problem. 所有这一切,我没有遇到你遇到的问题,这让我相信一些明智的布局管理器的使用将解决问题。

I solved this by setting the maximum width on the container of the text field, using setMaximumSize . 我通过使用setMaximumSize设置文本字段容器的最大宽度来解决这个问题。

According to davetron's answer, this is a fragile solution, because the layout manager might disregard that property. 根据davetron的回答,这是一个脆弱的解决方案,因为布局管理员可能会忽视该属性。 In my case, the container is the top-most, and in a first test it worked. 就我而言,容器是最顶层的,并且在第一次测试中它起作用。

Don't set any of the sizes on the text field. 不要在文本字段中设置任何大小。 Instead set the column size to a non-zero value via setColumns or using the constructor with the column argument. 而是通过setColumns或使用带有column参数的构造函数将列大小设置为非零值。

What is happening is that the preferred size reported by the JTextComponent when columns is zero is the entire amount of space needed to render the text. 发生的事情是,当列为零时,JTextComponent报告的首选大小是呈现文本所需的全部空间量。 When columns is set to a non-zero value the preferred size is the needed size to show that many standard column widths. 当列设置为非零值时,首选大小是显示许多标准列宽的所需大小。 (for a variable pitch font it is usually close to the size of the lower case 'm'). (对于可变间距字体,它通常接近小写字母“m”的大小)。 With columns set to zero the text field is requesting as much space as it can get and stretching out the whole container. 将列设置为零时,文本字段请求尽可能多的空间并扩展整个容器。

Since you already have it in a GridBagLayout with a fill, you could probably just set the columns to 1 and let the fill stretch it out based on the other components, or some other suitably low number. 由于你已经在GridBagLayout中使用了填充,你可能只需将列设置为1并让填充根据其他组件或其他适当的低数量将其拉伸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM