2011-02-25 163 views
0

我用Java編寫的東西,需要用戶輸入詢問有多少客戶在付款,然後當他們投入多少和擊去,它會創建中創建的變量名稱一個包含jcombobox,JLabel和JTextField的行,用於輸入用戶輸入的數字。我的問題是,我不能讓每一行出現在下一個下面,以及我無法弄清楚如何根據循環中的計數唯一地命名它們,以便我可以擁有唯一的動作偵聽器供以後使用。增加的計數值在for循環的這個循環

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

回答

0

製作它們的一個數組,並且像myArray[0]等地址。