【发布时间】: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”按钮的约束中。
-
效果很好,谢谢。对文本字段有任何想法吗?我也尝试在那里成长,但没有骰子,没有双关语。