我已经提到了这个我想通过单击添加按钮动态添加一个 JLabel 和文本框来动态创建我的JTextField
。
但是我如何使用getText()
来分别从每个文本字段中检索文本?
public class CreateGuestForm2 {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="111,10"
private JPanel jContentPane = null;
private int indexer = 1; // @jve:decl-index=0:
private List<JLabel> listOfLabels = new ArrayList<JLabel>(); // @jve:decl-index=0:
private List<JTextField> listOfTextFields = new ArrayList<JTextField>(); // @jve:decl-index=0:
private JButton jButtonAdd = null;
private JPanel jPanel = null;
private GridBagConstraints textFieldConstraints = new GridBagConstraints(); // @jve:decl-index=0:
private GridBagConstraints labelConstraints = new GridBagConstraints();// @jve:decl-index=0:
private JButton jButtonCreate = null;
private JButton jButtonHelp = null;
String textFields = null;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setLayout(new GridBagLayout());
jFrame.setPreferredSize(new Dimension(800, 600));
jFrame.setTitle("Create Guest Form");
jFrame.setSize(new Dimension(800, 582));
jFrame.setContentPane(getJContentPane());
jFrame.pack();
jFrame.setVisible(true);
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButtonAdd(), null);
jContentPane.add(getJPanel(), null);
jContentPane.add(getJButtonCreate(), null);
jContentPane.add(getJButtonHelp(), null);
jContentPane.add(getJPanel(), null);
jContentPane.add(getJPanel(), null);
}
return jContentPane;
}
/**
* This method initializes jButtonAdd
*
* @return javax.swing.JButton
*/
private JButton getJButtonAdd() {
if (jButtonAdd == null) {
jButtonAdd = new JButton();
jButtonAdd.setText("Add a guest");
jButtonAdd.setBounds(new Rectangle(14, 45, 111, 24));
jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()");
addGuest(e);
}
});
}
return jButtonAdd;
}
public void addGuest(ActionEvent e){
// Clear panel
jPanel.removeAll();
// Create label and text field
JTextField jTextField = new JTextField();
textFields = jTextField.getText();
jTextField.setSize(100, 200);
listOfTextFields.add(jTextField);
listOfLabels.add(new JLabel("Guest " + indexer));
// Add labels and text fields
for(int i = 0; i < indexer; i++)
{
// Text field constraints
textFieldConstraints.gridx = 1;
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.insets = new Insets(10, 10, 10, 10);
textFieldConstraints.gridy = i;
// Label constraints
labelConstraints.gridx = 0;
labelConstraints.gridy = i;
labelConstraints.insets = new Insets(10, 10, 10, 10);
// Add them to panel
jPanel.add(listOfLabels.get(i), labelConstraints);
jPanel.add(listOfTextFields.get(i), textFieldConstraints);
}
// Align components top-to-bottom
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = indexer;
c.weighty = 1;
jPanel.add(new JLabel(), c);
indexer++;
jPanel.updateUI();
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setPreferredSize(new Dimension(600,600));
jPanel.setBounds(new Rectangle(14, 76, 759, 413));
jPanel.setBorder(LineBorder.createBlackLineBorder());
}
return jPanel;
}
/**
* This method initializes jButtonCreate
*
* @return javax.swing.JButton
*/
private JButton getJButtonCreate() {
if (jButtonCreate == null) {
jButtonCreate = new JButton();
jButtonCreate.setText("Create");
jButtonCreate.setBounds(new Rectangle(681, 505, 93, 27));
jButtonCreate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
createGuest(e);
}
});
}
return jButtonCreate;
}
public void createGuest(ActionEvent e){
String record = textFields;
GuestControl gCreate = new GuestControl();
if(gCreate.processCreateGuest2(record)){
JOptionPane.showMessageDialog(null, "Guest record created successfully");
getJFrame().dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Guest not created, please try again");
}
}
}
ps:我正在为一个创建来宾表格的学校项目做这个