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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/128016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:37:03  来源:igfitidea点击:

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

javaswingjtextfield

提问by Leonel

I'm writing a custom file selection component. In my UI, first the user clicks a button, which pops a JFileChooser; when it is closed, the absolute path of the selected file is written to a JTextField.

我正在编写一个自定义文件选择组件。在我的 UI 中,首先用户单击一个按钮,该按钮弹出一个JFileChooser; 当它关闭时,所选文件的绝对路径被写入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 GridBagConstraintsattached 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?

那么,如何设置 a 的最大大小JTextField

采纳答案by davetron5000

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 JScrollPanes.

如果是这样,对于大多数合理的布局,该字段不应该改变大小(至少,它不应该增长)。通常布局管理器在放入JScrollPanes时表现不佳。

In my experience, trying to control the sizes via setMaximumSizeand setPreferredWidthand so on are precarious at best. Swing decided on its own with the layout manager and there's little you can do about it.

根据我的经验,试图通过控制尺寸setMaximumSizesetPreferredWidth等充其量岌岌可危。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.

尽管如此,我没有遇到您遇到的问题,这让我相信明智地使用布局管理器将解决问题。

回答by Leonel

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. In my case, the container is the top-most, and in a first test it worked.

根据 davetron 的回答,这是一个脆弱的解决方案,因为布局管理器可能会忽略该属性。就我而言,容器是最顶层的,并且在第一次测试中它起作用了。

回答by shemnon

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. 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'). With columns set to zero the text field is requesting as much space as it can get and stretching out the whole container.

发生的情况是,当列为零时 JTextComponent 报告的首选大小是呈现文本所需的全部空间量。当列设置为非零值时,首选大小是显示许多标准列宽所需的大小。(对于可变间距字体,它通常接近小写“m”的大小)。当列设置为零时,文本字段请求尽可能多的空间并拉伸整个容器。

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 并让填充根据其他组件或其他适当的低数字将其拉伸。