Java 如何在对话框窗口中显示表单?

Java 如何在对话框窗口中显示表单?,java,swing,user-interface,Java,Swing,User Interface,我有一个简单的表单,由3个文本字段和一个取消/确定按钮组成。这是一个组布局。我想知道如何使此表单在新窗口中弹出,类似于对话框?我可以将GroupLayout传递给JDialog吗?我还需要在表单窗口关闭之前验证输入。我目前有这个工作,但它使用了一个新的框架,这样做,我读到,这不是这样做的方式,所以我在这里问 这是我的密码: import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; i

我有一个简单的表单,由3个文本字段和一个取消/确定按钮组成。这是一个组布局。我想知道如何使此表单在新窗口中弹出,类似于对话框?我可以将GroupLayout传递给JDialog吗?我还需要在表单窗口关闭之前验证输入。我目前有这个工作,但它使用了一个新的框架,这样做,我读到,这不是这样做的方式,所以我在这里问

这是我的密码:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.*;

public class AddStockPromptTest {

    private JFrame frame;
    private JPanel mainPanel;
    private JPanel formPanel;
    private JPanel buttonPanel;
    //private GroupLayout layout;
    private JLabel lblItem;
    private JLabel lblPrice;
    private JLabel lblQuantity;
    private JTextField itemField;
    private JTextField priceField;
    private JTextField quantityField;
    private JButton okBtn;
    private JButton cancelBtn;

    public AddStockPromptTest() {
        frame = new JFrame("Add New Stock Item");
        //Container contentPane = frame.getContentPane();

        mainPanel = new JPanel(new BorderLayout());
        buttonPanel = new JPanel(new FlowLayout());

        formPanel = new JPanel();

        GroupLayout groupLayout = new GroupLayout(formPanel);
        formPanel.setLayout(groupLayout);
        groupLayout.setAutoCreateGaps(true);

        lblItem = new JLabel("Item:");
        lblPrice = new JLabel("Price:");
        lblQuantity = new JLabel("Quantity:");

        itemField = new JTextField();
        priceField = new JTextField();
        quantityField = new JTextField();

        groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
                .addGroup(groupLayout.createParallelGroup()
                        .addComponent(lblItem)
                        .addComponent(lblPrice)
                        .addComponent(lblQuantity))
                .addGroup(groupLayout.createParallelGroup()
                        .addComponent(itemField)
                        .addComponent(priceField)
                        .addComponent(quantityField))
        );

        groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
                .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblItem)
                        .addComponent(itemField))
                .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPrice)
                        .addComponent(priceField))
                .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblQuantity)
                        .addComponent(quantityField))
        );


        cancelBtn = new JButton("Cancel");
        buttonPanel.add(cancelBtn);

        okBtn = new JButton("OK");
        buttonPanel.add(okBtn);


        mainPanel.add(formPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        frame.add(mainPanel);


        frame.setTitle("Basil's Pizza Ordering System");
        frame.setSize(800, 600);
        frame.pack();
        frame.setVisible(true);

        //addStockPrompt();
    }

    private void addStockPrompt() {

    }

}

JOptionPane将为您提供一个对话框。在“确定”按钮的执行事件中使用此选项

JOptionPane.showMessageDialog(null, message);

“我想知道如何在类似对话框的新窗口中弹出此表单?”使用
JDialog
JOptionPane
。请参见示例7.
JOptionPane.showMessageDialog(null,formPanel,“form”,JOptionPane.INFORMATION\u MESSAGE)