【问题标题】:MigLayout: Put two JLabel on the same line separated by JTextFieldMigLayout:将两个 JLabel 放在由 JTextField 分隔的同一行上
【发布时间】:2019-02-16 07:59:09
【问题描述】:
如何在同一行中设置两个由 JTextField 分隔的 JLabel?我正在使用 MigLayout。我有以下代码
JPanel helper = new JPanel( new MigLayout() );
helper.add( new JLabel( "Threshold:" ), "" );
threshold = new JTextField();
Icon thresholdIcon = UIManager.getIcon( "OptionPane.questionIcon" );
JLabel thresholdIconLabel = new JLabel( thresholdIcon );
thresholdIconLabel.setToolTipText( "Threshold for template matching" );
helper.add( threshold, "wrap, width 100:20" );
helper.add( thresholdIconLabel, "wrap, width 100:20" );
我得到的输出如下所示。
我希望图标与阈值和文本字段位于同一行。我应该如何调整它?
感谢您提供任何帮助/建议。
【问题讨论】:
标签:
java
user-interface
jlabel
jtextfield
miglayout
【解决方案1】:
您是否考虑过在放置组件时使用行/列约束并使用“单元格”参数?我用这种方法取得了很大的成功。
JPanel helper = new JPanel(
new MigLayout(
"",
"5[grow,fill]10[grow,fill,20:100]10[grow,fill,20:100]5",
"5[fill,grow]5"));
helper.add( new JLabel( "Threshold:" ), "cell 0 0" );
threshold = new JTextField(); // Assuming this was declared earlier
Icon thresholdIcon = UIManager.getIcon( "OptionPane.questionIcon" );
JLabel thresholdIconLabel = new JLabel( thresholdIcon );
thresholdIconLabel.setToolTipText( "Threshold for template matching" );
helper.add( threshold, "cell 1 0" );
helper.add( thresholdIconLabel, "cell 2 0" );
请务必阅读 MigLayout Whitepaper 和 MigLayout Quick Start Guide,因为它们很好地解释了您可以使用的所有内容。
旁注:
它没有出现在一行中的原因是因为您告诉 MigLayout 可以wrap 组件以适应您给它的空间,包括创建新的行来适应。您也可以尝试增加窗口的大小。
100:20 的大小转换为 100 像素的最小大小,但首选大小为 20 像素,我不知道 MigLayout 会如何处理。我在我的代码中更改了它。
(免责声明:代码未经测试,可能有点粗糙)