将for循环中的计数值添加到该循环中创建的变量名称

时间:2011-02-25 21:28:31

标签: java variables for-loop naming-conventions

我正在用Java编写一些东西,用户输入询问有多少客户付款,然后当他们输入多少并点击时,它会创建一个包含jcombobox,JLabel和JTextField的行,用户输入的数字。我的问题是我不能让每一行显示在下一行下面,而且我无法弄清楚如何根据循环中的计数对它们进行唯一命名,这样我就可以拥有独特的动作侦听器供以后使用。

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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

制作一组数组并像myArray[0]一样解决它们等等。

答案 1 :(得分:0)