我有一个简单的表单,包含3个文本字段和一个取消/确定按钮。这是在GroupLayout中。我想知道如何在类似于对话框的新窗口中弹出此表单?我可以将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() {
}
}
答案 0 :(得分:0)
JOptionPane将为您提供一个对话框。在okay按钮的actionperformed事件中使用它。
JOptionPane.showMessageDialog(null, message);