简体   繁体   English

GridBagLayout管理器和调整大小控件

[英]GridBagLayout manager and resizing controls

I'm not sure if GridBagLayoutManager is the only layout manager that does this, but here is my problem. 我不确定GridBagLayoutManager是否是唯一执行此操作的布局管理器,但这是我的问题。 I have 4 controls layed out horizontally in a GridBagLayout. 我在GridBagLayout中有4个水平放置的控件。 To keep things simple for this example, each control get's an equal 1/4 of the form, and each control resizes with the same ratio as the other controls. 为了使这个例子简单,每个控件得到的形状相等1/4,每个控件的调整大小与其他控件相同。 The four controls are JTextField's where each text field maps to a column in a record from a ResultSet. 这四个控件是JTextField,其中每个文本字段映射到ResultSet中记录中的列。 Additional controls on the form allow one to navigate through the records. 表单上的其他控件允许用户浏览记录。

If I navigate from one record to the next, then the text fields update their text to show the new data. 如果我从一个记录导航到下一个记录,则文本字段将更新其文本以显示新数据。 However, the text fields also get automatically resized in proportion to the amount of text they are showing... text fields with a large amount of data expand in size, and text fields with a smaller amount of data get squished. 但是,文本字段也会根据显示的文本量自动调整大小...大量数据的文本字段大小扩大,数据量较小的文本字段会被压缩。 If I run through 10 records in a ResultSet, then the controls are always resizing themselves and it looks quite bizarre to say the least. 如果我在ResultSet中运行10条记录,那么控件总是调整自己的大小,至少可以说它看起来很奇怪。

What I would like to do is prevent these controls from resizing, unless (and until) the underlying container gets resized. 我想要做的是防止这些控件调整大小,除非(并且直到)底层容器调整大小。 So if I resize the window, then I would like the controls to resize (according to the "weight x" variable in the Layout), but I don't want the controls to resize just because the amount of text they are showing becomes more or less. 因此,如果我调整窗口大小,那么我希望控件调整大小(根据布局中的“权重x”变量),但我不希望控件调整大小只是因为它们显示的文本量变得更多或更少。

Anybody have any ideas here? 有人在这有什么想法吗?

For JTextField (as mentioned in the contents) call setColumns(int) to set a preferred size on the text. 对于JTextField (如内容中所述),调用setColumns(int)以在文本上设置首选大小。

For JComboBox , call setPrototypeDisplayValue(Object) which will cause that value to be rendered and the preferred size of the JComboBox will be set based on that value. 对于JComboBox ,调用setPrototypeDisplayValue(Object)将导致该值被渲染,并且将根据该值设置JComboBox的首选大小。

In general, you can call setPreferredSize(Dimension) on any component directly to get the same behavior. 通常,您可以直接在任何组件上调用setPreferredSize(Dimension)来获得相同的行为。 General if not set the value is calculated based on some defaults on the component. 常规如果未设置,则根据组件的某些默认值计算该值。 What is happening with JTextField , JComboBox , and most JTextComponent derivatives. JTextFieldJComboBox和大多数JTextComponent衍生产品正在发生什么。 is that the preferred size on those components is driven by values the user is capable of changing (the text values, the combobox selection). 这些组件的首选大小是由用户能够更改的值(文本值,组合框选择)驱动的。 Whereas with most other component ( JButton , JCheckBox , etc) the content size doesn't really change when the user acts on it. 而对于大多数其他组件( JButtonJCheckBox等),当用户对其进行操作时,内容大小并没有真正改变。 Setting the columns and rows and the prototype display value fixes the value used to calculate the preferred size. 设置列和行以及原型显示值可修复用于计算首选大小的值。

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

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