adding the count value in a for loop to the name of variables created within that loop
I am writing something in Java that takes user input asking how many customers are making payments, then when they input how many and hit go, it creates a row containing a jcombobox, JLabel, and JTextField, for the number the user inputs. My problem is that I cant get each row to appear under the next as well as I cant figure out how to name them each uniquely based on the count in the loop so that I can have unique action listeners to them for later use.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MacAdvantageGui extends JFrame implements ActionListener {
//MacEvents macEvent = new MacEvents(this);
// Setting up buttons and such
JPanel row1= new JPanel();
JPanel row2= new JPanel();
JPanel row3= new JPanel();
JLabel payNumb = new JLabel("How many payments are being made this "
+ " month开发者_如何学运维?", JLabel.RIGHT);
JTextField numbPayments = new JTextField("0",3);
JButton goButton = new JButton("GO");
JButton addCust = new JButton("Add New Customer");
JButton print = new JButton("Print");
JButton printPast = new JButton("Print Past Payments");
public MacAdvantageGui() {
super(" "
+ "Mac Advantage Customer Payments");
setSize(650, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setResizable(false);
setVisible(true);
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
setLayout(layout);
// Add listeners
addCust.addActionListener(this);
goButton.addActionListener(this);
numbPayments.addActionListener(this);
//Setting up layout and adding buttons and such
FlowLayout flo1 = new FlowLayout(FlowLayout.LEFT);
row1.setLayout(flo1);
row1.add(payNumb);
row1.add(numbPayments);
row1.add(goButton);
add(row1);
FlowLayout flo2 = new FlowLayout();
row2.setLayout(flo2);
add(row2);
FlowLayout flo3 = new FlowLayout(FlowLayout.LEFT);
row3.setLayout(flo3);
row3.add(addCust);
add(row3);
setVisible(true);
}
public void actionPerformed(ActionEvent a) {
String command = a.getActionCommand();
if (command.equals("Add New Customer")){
AddCustomer addCustomer = new AddCustomer();
}
if (command.equals("GO")){
int numPay = Integer.parseInt(numbPayments.getText());
System.out.print("go! " + numPay);
for (int count = 1; count <= numPay; count++){
String countString = Integer.toString(count);
JComboBox custData = new JComboBox();
JPanel row2a= new JPanel();
custData.addItem("Please Select a Customer from the "
+ "dropdown menu");
custData.addItem("1");
custData.addItem("2");
JLabel dollar = new JLabel("$");
JTextField payAmount = new JTextField(4);
row2a.add(custData);
row2a.add(dollar);
row2a.add(payAmount);
row2.add(row2a);
row2.revalidate();
}
}
}
Make an array of them and address them like myArray[0]
and so on.
Try to use JTable . http://www.javalobby.org/articles/jtable/.
精彩评论