Swing GroupLayout:调整和限制组件大小 [英] Swing GroupLayout: Resizing and limiting component sizes
问题描述
我正在使用 GroupLayout
来管理一些动态生成的数据输入表单中的组件。布局或多或少如此:
I'm using GroupLayout
to manage components in some dynamically generated data input forms. The layout is more or less like so:
*-----------------------------------------------*
| label A | field A |
| label B | field B |
| label C | field C |
*-----------------------------------------------*
我使用2个并行组作为水平布局,并使用单个顺序组垂直布局。在大多数情况下,一切都运行得很好。
I'm using 2 parallel groups for the horizontal layout, and a single sequential group for the vertical layout. For the most part, everything is working just fine.
我想限制标签的最大宽度(这只是 JLabel的实例
)到父 JFrame
宽度的1/3。如果 JFrame
是固定大小,这将是微不足道的,但我必须处理它被调整大小。
I want to limit the maximum width of the labels (which are just instances of JLabel
) to 1/3 of the width of the parent JFrame
. If the JFrame
was a fixed size this would be trivial, but I have to deal with it being resized.
我从 JFrame
中拿起 ComponentListener.componentResized()
事件,但我有点卡在什么上我曾经收到过这样的活动。
I'm picking up ComponentListener.componentResized()
events from the JFrame
but I'm a bit stuck on what to do once I receive such an event.
我试过这种方法没有任何运气:
I've tried this approach without any luck:
public void componentResized(ComponentEvent e) {
int maxW = parentFrame.getWidth() / 3;
for (JLabel l : labels) {
l.setMaximumSize( // have also tried setSize() and setPreferredSize()
new Dimension(
Math.min(l.getSize().width, maxW),
l.getMaximumSize().height));
}
groupLayout.invalidateLayout(getContentSpace());
}
任何人都可以建议一种限制标签宽度的方法会有效吗?
我每次都可以从头开始重建布局,但我觉得应该有一种更简单的方法。
I could probably rebuild the layout from scratch every time, but I feel like there ought to be a simpler way.
推荐答案
在这个例子中, GroupLayout
可以简单地依赖于标签的首选大小,不得不求助于任何 setXXXSize()
方法。在这种方法中,
In this example, GroupLayout
can simply rely on the preferred size of the label, without having to resort to any setXXXSize()
method. In this approach,
- 标签的空间总是容纳最大的标签。
- 文本字段可以通过有用的方式调整大小。
- 跨平台和字体的结果是正确的。
- 您不需要为大多数字段指定任何内容组件...因为组件本身具有默认的所需调整大小行为。 - 如何使用GroupLayout:组件大小和可重新定位
- The space for the labels always accommodates the largest label.
- The text fields are resizable in a useful way.
- The result is correct across platforms and fonts.
- "You do not need to specify anything for most of the components…because the components themselves have the desired resizing behavior as default."—How to Use GroupLayout: Component Size and Resizability
使用 GroupLayout.Alignment.TRAILING
右边对齐标签是个人偏好,我添加了第二个面板来展示它是如何嵌套在另一个布局中的。
The use of GroupLayout.Alignment.TRAILING
to right justify the labels is a personal preference, and I've added a second panel to show how it works nested in another layout.
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/** @see http://stackoverflow.com/questions/8492065 */
public class GroupPanel extends JPanel {
private JLabel label1 = new JLabel("Primary:");
private JTextField field1 = new JTextField(16);
private JLabel label2 = new JLabel("Secondary:");
private JTextField field2 = new JTextField(16);
private JLabel label3 = new JLabel("Tertiary:");
private JTextField field3 = new JTextField(16);
public GroupPanel(int n) {
this.setBorder(BorderFactory.createTitledBorder("Panel " + n));
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(label1)
.addComponent(label2)
.addComponent(label3))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(field1)
.addComponent(field2)
.addComponent(field3))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(field1))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(field2))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label3)
.addComponent(field3))
);
}
private static void display() {
JFrame f = new JFrame("GroupPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
f.add(new GroupPanel(1));
f.add(new GroupPanel(2));
f.add(Box.createVerticalGlue());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
这篇关于Swing GroupLayout:调整和限制组件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!