如何在Java代码中制作输入表单(不是使用JForm的Netbeans)?

2022-09-03 05:41:59

我想在Java中创建一个输入表单,以便用户可以输入详细信息。

像这样:

desired form


我的代码

import java.awt.GridLayout;
import javax.swing.*;

class JOptionPaneTest {

public static void main(String[] args) {
    String[] items = {"One", "Two", "Three", "Four", "Five"};
    JComboBox combo = new JComboBox(items);
    JTextField field1 = new JTextField("1234.56");
    JTextField field2 = new JTextField("9876.54");
    JPanel panel = new JPanel(new GridLayout(0, 1));
    panel.add(combo);
    panel.add(new JLabel("Field 1:"));
    panel.add(field1);
    panel.add(new JLabel("Field 2:"));
    panel.add(field2);
   int result = JOptionPane.showConfirmDialog(null, panel, "Test",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (result == JOptionPane.OK_OPTION) {
        System.out.println(combo.getSelectedItem()
            + " " + field1.getText()
            + " " + field2.getText());
    } else {
        System.out.println("Cancelled");
    }
}
}

我的输出表单:

current state of form

我想我必须将我的布局更改为类似.任何想法如何获得问题顶部的表单的外观?BorderLayout


答案 1

是的,您必须更改布局。看看SpringLayout和这个例子:

alt text
(资料来源:sun.com

String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
int numPairs = labels.length;

//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < numPairs; i++) {
    JLabel l = new JLabel(labels[i], JLabel.TRAILING);
    p.add(l);
    JTextField textField = new JTextField(10);
    l.setLabelFor(textField);
    p.add(textField);
}

//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
                                numPairs, 2, //rows, cols
                                6, 6,        //initX, initY
                                6, 6);       //xPad, yPad

SpringLayout适用于这种简单的表单,但是有第三方库具有更多功能。即米格布局


答案 2

使用 GridBagLayout 创建窗体的另一种方法,生成以下结果:

enter image description here

法典:

JPanel addressPanel = new JPanel();
Border border = addressPanel.getBorder();
Border margin = new EmptyBorder(10, 10, 10, 10);
addressPanel.setBorder(new CompoundBorder(border, margin));

GridBagLayout panelGridBagLayout = new GridBagLayout();
panelGridBagLayout.columnWidths = new int[] { 86, 86, 0 };
panelGridBagLayout.rowHeights = new int[] { 20, 20, 20, 20, 20, 0 };
panelGridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
panelGridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
                                               Double.MIN_VALUE };
addressPanel.setLayout(panelGridBagLayout);

addLabelAndTextField("City:", 0, addressPanel);
addLabelAndTextField("Street:", 1, addressPanel);
addLabelAndTextField("State:", 2, addressPanel);
addLabelAndTextField("Phone:", 3, addressPanel);
addLabelAndTextField("Mail:", 4, addressPanel);

帮助程序方法:addLabelAndTextField

private void addLabelAndTextField(String labelText, int yPos,
                                  Container containingPanel) {

    JLabel label = new JLabel(labelText);
    GridBagConstraints gridBagConstraintForLabel = new GridBagConstraints();
    gridBagConstraintForLabel.fill = GridBagConstraints.BOTH;
    gridBagConstraintForLabel.insets = new Insets(0, 0, 5, 5);
    gridBagConstraintForLabel.gridx = 0;
    gridBagConstraintForLabel.gridy = yPos;
    containingPanel.add(label, gridBagConstraintForLabel);

    JTextField textField = new JTextField();
    GridBagConstraints gridBagConstraintForTextField = new GridBagConstraints();
    gridBagConstraintForTextField.fill = GridBagConstraints.BOTH;
    gridBagConstraintForTextField.insets = new Insets(0, 0, 5, 0);
    gridBagConstraintForTextField.gridx = 1;
    gridBagConstraintForTextField.gridy = yPos;
    containingPanel.add(textField, gridBagConstraintForTextField);
    textField.setColumns(10);
}

推荐