如何在同一行中设置由 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" );
我得到的输出如下所示。
我希望图标与阈值和文本字段位于同一行。我应该如何调整呢? 如有任何帮助/建议,我们将不胜感激。
最佳答案
您是否考虑过在放置组件时使用行/列约束并使用“单元格”参数?我用这种方法取得了很大的成功。
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 可以
换行
组件以适应你给它的空间,包括制作新行以适应。您也可以尝试增加窗口的大小。尺寸
100:20
转换为最小尺寸 100 像素,但首选尺寸为 20 像素,我不知道 MigLayout 将如何处理这个问题。我在代码中更改了它。
(免责声明:代码未经测试,可能有点粗糙)
关于java - MigLayout:将两个JLabel放在同一行,用JTextField分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52282263/