我想通过单击添加按钮动态添加JLabel和文本框

当我动态创建文本和标签框时,它应该采用“Textbox:Labelbox”的格式,然后当我再次点击添加按钮时,相同的模式应该在下一行重复,依此类推……我应该使用哪种布局以及如何使用?

这是我使用的代码

if(field_name.getText().equals("")){ error.setForeground(Color.red); error.setText("Enter the Field name first"); } else { JLabel l = new JLabel(field_name.getText(), JLabel.RIGHT); JTextField textField = new JTextField(); Dimension dim = new Dimension(20,30); textField.setPreferredSize(dim); field_layer.add(l); field_layer.add(textField); SpringUtilities.makeCompactGrid(field_layer, numPairs, 2, //rows, cols 6, 6, //initX, initY 6, 6); //xPad, yPad numPairs++; field_layer.invalidate(); this.pack(); } 

一个选项是GridBagLayout 。 为了正确使用此布局,您需要了解GridBagConstraints 。 这是一个帮助您入门的教程 。

这是一个简单的例子:

 import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class MyExample { // Field members static JPanel panel = new JPanel(); static Integer indexer = 1; static List listOfLabels = new ArrayList(); static List listOfTextFields = new ArrayList(); public static void main(String[] args) { // Construct frame JFrame frame = new JFrame(); frame.setLayout(new GridBagLayout()); frame.setPreferredSize(new Dimension(300, 300)); frame.setTitle("My Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame constraints GridBagConstraints frameConstraints = new GridBagConstraints(); // Construct button JButton addButton = new JButton("Add"); addButton.addActionListener(new ButtonListener()); // Add button to frame frameConstraints.gridx = 0; frameConstraints.gridy = 0; frame.add(addButton, frameConstraints); // Construct panel panel.setPreferredSize(new Dimension(200, 200)); panel.setLayout(new GridBagLayout()); panel.setBorder(LineBorder.createBlackLineBorder()); // Add panel to frame frameConstraints.gridx = 0; frameConstraints.gridy = 1; frameConstraints.weighty = 1; frame.add(panel, frameConstraints); // Pack frame frame.pack(); // Make frame visible frame.setVisible(true); } static class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { // Clear panel panel.removeAll(); // Create label and text field listOfTextFields.add(new JTextField()); listOfLabels.add(new JLabel("Label " + indexer)); // Create constraints GridBagConstraints textFieldConstraints = new GridBagConstraints(); GridBagConstraints labelConstraints = new GridBagConstraints(); // Add labels and text fields for(int i = 0; i < indexer; i++) { // Text field constraints textFieldConstraints.gridx = 0; textFieldConstraints.gridy = i; // Label constraints labelConstraints.gridx = 1; labelConstraints.gridy = i; // Add them to panel panel.add(listOfTextFields.get(i), textFieldConstraints); panel.add(listOfLabels.get(i), labelConstraints); } // Align components top-to-bottom GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = indexer; c.weighty = 1; panel.add(new JLabel(), c); // Increment indexer indexer++; } } } 

注意:不要仅限于此特定布局管理器。 也就是说,您也应该探索其他布局管理器 。

我对上面的代码做了一些改动…..

  import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class MyExample { // Field members static JPanel panel = new JPanel(); static Integer indexer = 1; static List listOfLabels = new ArrayList(); static List listOfTextFields = new ArrayList(); public static void main(String[] args) { // Construct frame JFrame frame = new JFrame(); frame.setLayout(new GridBagLayout()); frame.setPreferredSize(new Dimension(990, 990)); frame.setTitle("My Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame constraints GridBagConstraints frameConstraints = new GridBagConstraints(); // Construct button JButton addButton = new JButton("Add"); addButton.addActionListener(new ButtonListener()); // Add button to frame frameConstraints.gridx = 0; frameConstraints.gridy = 0; frame.add(addButton, frameConstraints); // Construct panel panel.setPreferredSize(new Dimension(600, 600)); panel.setLayout(new GridBagLayout()); panel.setBorder(LineBorder.createBlackLineBorder()); // Add panel to frame frameConstraints.gridx = 0; frameConstraints.gridy = 1; frameConstraints.weighty = 1; frame.add(panel, frameConstraints); // Pack frame frame.pack(); // Make frame visible frame.setVisible(true); } static class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { // Clear panel panel.removeAll(); // Create label and text field JTextField jTextField = new JTextField(); jTextField.setSize(100, 200); listOfTextFields.add(jTextField); listOfLabels.add(new JLabel("Label " + indexer)); // Create constraints GridBagConstraints textFieldConstraints = new GridBagConstraints(); GridBagConstraints labelConstraints = new GridBagConstraints(); // 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 panel.add(listOfLabels.get(i), labelConstraints); panel.add(listOfTextFields.get(i), textFieldConstraints); } // Align components top-to-bottom GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = indexer; c.weighty = 1; panel.add(new JLabel(), c); // Increment indexer indexer++; panel.updateUI(); } } } 

我推荐一个GridBagLayout。 您可以使用:

 JPanel pan = new JPanel(); // For Swing //or Panel pan = new Panel(); for AWT pan.setLayout(new GridBagLayout()); GridBagConstraints textC = new GridBagConstraints(); textC.gridx = 0; GridBagConstraints labelC= new GridBagConstraints(); labelC.gridx = 1; // For Every (J)TextField pan.add(text,textC); // For Every (J)Label label.add(label,labelC); 

我修改一些代码和

看到这个包formbuilder;

  import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class CreateFormFields { // Field members static JPanel panel = new JPanel(); static Integer indexer = 1; static List listOfLabels = new ArrayList(); static List listOfTextFields = new ArrayList(); static List listDataType = new ArrayList(); static List listRange = new ArrayList(); static List listLable = new ArrayList(); public static void main(String[] args) { // Construct frame JFrame frame = new JFrame(); frame.setLayout(new GridBagLayout()); frame.setPreferredSize(new Dimension(990, 990)); frame.setTitle("Form Creator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame constraints GridBagConstraints frameConstraints = new GridBagConstraints(); // Construct button JButton addButton = new JButton("Add"); JButton createButton = new JButton("Create Form"); addButton.addActionListener(new ButtonListener()); createButton.addActionListener(new CreateForm()); // Add button to frame frameConstraints.gridx = 0; frameConstraints.gridy = 0; frame.add(addButton, frameConstraints); frameConstraints.gridx = 0; frameConstraints.gridy = 1; frame.add(createButton, frameConstraints); // Construct panel panel.setPreferredSize(new Dimension(900, 900)); panel.setLayout(new GridBagLayout()); panel.setBorder(LineBorder.createBlackLineBorder()); // Add panel to frame frameConstraints.gridx = 0; frameConstraints.gridy = 2; frameConstraints.weighty = 1; frame.add(panel, frameConstraints); // Pack frame frame.pack(); // Make frame visible frame.setVisible(true); } static class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { // Clear panel panel.removeAll(); // Create label and text field JTextField jTextField = new JTextField(); jTextField.setSize(5, 20); JTextField jTextField2 = new JTextField(); jTextField2.setSize(6, 20); JTextField jTextField3 = new JTextField(); jTextField3.setSize(7, 20); JTextField jTextField4 = new JTextField(); jTextField4.setSize(8, 20); listOfTextFields.add(jTextField); listDataType.add(jTextField2); listRange.add(jTextField3); listLable.add(jTextField4); listOfLabels.add(new JLabel("LableName | FieldName | DataType | Range ")); // Create constraints GridBagConstraints textFieldConstraints = new GridBagConstraints(); GridBagConstraints labelConstraints = new GridBagConstraints(); GridBagConstraints gridlistDataType = new GridBagConstraints(); GridBagConstraints gridlistRange = new GridBagConstraints(); GridBagConstraints gridlistLable = new GridBagConstraints(); // Add labels and text fields for (int i = 0; i < indexer; i++) { // Label constraints labelConstraints.gridx = 0; labelConstraints.gridy = i; labelConstraints.insets = new Insets(10, 10, 10, 10); // Text field constraints textFieldConstraints.gridx = 1; textFieldConstraints.fill = 1;//GridBagConstraints.HORIZONTAL; textFieldConstraints.weightx = 0.5; textFieldConstraints.insets = new Insets(10, 10, 10, 10); textFieldConstraints.gridy = i; gridlistDataType.gridx = 2; gridlistDataType.fill = 1;//GridBagConstraints.HORIZONTAL; gridlistDataType.weightx = 0.5; gridlistDataType.insets = new Insets(10, 10, 10, 10); gridlistDataType.gridy = i; gridlistRange.gridx = 3; gridlistRange.fill = 1;//GridBagConstraints.HORIZONTAL; gridlistRange.weightx = 0.5; gridlistRange.insets = new Insets(10, 10, 10, 10); gridlistRange.gridy = i; gridlistLable.gridx = 4; gridlistLable.fill = 1;//GridBagConstraints.HORIZONTAL; gridlistLable.weightx = 0.5; gridlistLable.insets = new Insets(10, 10, 10, 10); gridlistLable.gridy = i; // Add them to panel panel.add(listOfLabels.get(i), labelConstraints); panel.add(listOfTextFields.get(i), textFieldConstraints); panel.add(listDataType.get(i), gridlistDataType); panel.add(listRange.get(i), gridlistRange); panel.add(listLable.get(i), gridlistLable); } // Align components top-to-bottom GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = indexer; c.weighty = 1; panel.add(new JLabel(), c); // Increment indexer indexer++; panel.updateUI(); } } static class CreateForm implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { // Clear panel panel.removeAll(); // Create label and text field JTextField jTextField = new JTextField(); jTextField.setSize(5, 20); JTextField jTextField2 = new JTextField(); jTextField2.setSize(6, 20); JTextField jTextField3 = new JTextField(); jTextField3.setSize(7, 20); JTextField jTextField4 = new JTextField(); jTextField4.setSize(8, 20); listOfTextFields.add(jTextField); listDataType.add(jTextField2); listRange.add(jTextField3); listLable.add(jTextField4); listOfLabels.add(new JLabel("LableName | FieldName | DataType | Range ")); // Create constraints GridBagConstraints textFieldConstraints = new GridBagConstraints(); GridBagConstraints labelConstraints = new GridBagConstraints(); GridBagConstraints gridlistDataType = new GridBagConstraints(); GridBagConstraints gridlistRange = new GridBagConstraints(); GridBagConstraints gridlistLable = new GridBagConstraints(); // Add labels and text fields for (int i = 0; i < indexer; i++) { // Label constraints labelConstraints.gridx = 0; labelConstraints.gridy = i; labelConstraints.insets = new Insets(10, 10, 10, 10); // Text field constraints textFieldConstraints.gridx = 1; textFieldConstraints.fill = 1;//GridBagConstraints.HORIZONTAL; textFieldConstraints.weightx = 0.5; textFieldConstraints.insets = new Insets(10, 10, 10, 10); textFieldConstraints.gridy = i; gridlistDataType.gridx = 2; gridlistDataType.fill = 1;//GridBagConstraints.HORIZONTAL; gridlistDataType.weightx = 0.5; gridlistDataType.insets = new Insets(10, 10, 10, 10); gridlistDataType.gridy = i; gridlistRange.gridx = 3; gridlistRange.fill = 1;//GridBagConstraints.HORIZONTAL; gridlistRange.weightx = 0.5; gridlistRange.insets = new Insets(10, 10, 10, 10); gridlistRange.gridy = i; gridlistLable.gridx = 4; gridlistLable.fill = 1;//GridBagConstraints.HORIZONTAL; gridlistLable.weightx = 0.5; gridlistLable.insets = new Insets(10, 10, 10, 10); gridlistLable.gridy = i; // Add them to panel panel.add(listOfLabels.get(i), labelConstraints); panel.add(listOfTextFields.get(i), textFieldConstraints); panel.add(listDataType.get(i), gridlistDataType); panel.add(listRange.get(i), gridlistRange); panel.add(listLable.get(i), gridlistLable); } // Align components top-to-bottom GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = indexer; c.weighty = 1; panel.add(new JLabel(), c); // Increment indexer indexer++; panel.updateUI(); } } }