【问题标题】:JTextField not fillling up column as expected?JTextField 没有按预期填满列?
【发布时间】:2021-10-29 10:30:48
【问题描述】:

学习使用 MigLayout。我正在尝试制作一个非常简单的表单,它是两列,一个标签,一个文本输入,重复,直到底部的提交按钮应该跨越两列。但是,它似乎没有按预期工作。文本字段没有展开,按钮也没有。看起来像这样

这是我的代码 -

package com.mypackage;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import net.miginfocom.swing.MigLayout;

public class DiceOddCalculator extends JFrame {
    public DiceOddCalculator(String title) {
        this.setSize(500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle(title);

        //Layout and panel to house components
        MigLayout layout = new MigLayout("wrap 2, debug");
        JPanel panel = new JPanel(layout);

        //Components
        JLabel maxDiceLabel = new JLabel("Max value of die");
        JTextField maxDiceInput = new JTextField();
        JLabel protectionCriteriaLabel = new JLabel("Die protection Criteria:");
        JTextField protectionCriteriaInput = new JTextField("");
        JLabel numOfDiceLabel = new JLabel("Number of dice:");
        JTextField numOfDiceInput = new JTextField("");
        JButton addDice = new JButton("Add dice");

        //add to panel
        panel.add(maxDiceLabel);
        panel.add(maxDiceInput);
        panel.add(protectionCriteriaLabel);
        panel.add(protectionCriteriaInput);
        panel.add(numOfDiceLabel);
        panel.add(numOfDiceInput);
        panel.add(addDice, "span");

        //Turn window on
        this.setContentPane(panel);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        DiceOddCalculator f = new DiceOddCalculator("Dice Odds Simulator");
    }
}

需要在我的代码中进行哪些更改以使 JTextField 和 Button 具有适当的宽度?

【问题讨论】:

  • 将“grow”添加到“addDice”按钮的约束中。
  • 效果很好,谢谢。对文本字段有任何想法吗?我也尝试在那里成长,但没有骰子,没有双关语。

标签: java miglayout


【解决方案1】:

我发现这与我构建 MigLayout 的方式有关。使用 MigLayout layout = new MigLayout("wrap 2, debug", "[fill, grow]", "") 使组件像我预期的那样横跨其列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多