我花了一些时间尝试不同的事情,试图让这个功课正常工作,但我无法弄明白,这是我认为最后一部分是在盯着我。当我输入名字和姓氏并按下添加帐户,然后确认它应该向一个arraylist添加一个帐户然后当我按下帐户号时它应该显示我总共有多少帐户,但它会一直显示0
BasicAccountList
import java.util.*;
public class BasicAccountList
{
private ArrayList < BasicAccount> accounts;
/**
* Create a BasicAccount.
*/
public BasicAccountList()
{
accounts = new ArrayList < BasicAccount>();
}
/**
* Add an account to this account list.
* @param account the accountobject to be added
*/
public void addAccount(BasicAccount account)
{
accounts.add(account);
}
/**
* Return the number of accounts currently held.
*
* @return the number of accounts
*/
public int getNumberOfAccounts()
{
return accounts.size();
}
}
BasicAccount
public class BasicAccount
{
private Name name;
private String accountNumber;
/**
* Constructor for objects of class Account.
* The number of pointsHeld should should be set to
* the supplied value.
*
* @param fName The Account Holder's first name
* @param lName The Account Holder's last name
* @param acctNumber The account number
*/
public BasicAccount(String fName, String lName, String acctNumber)
{
name = new Name (fName, lName);
accountNumber = acctNumber;
}
// accessors
/**
* Get the Account Holder's first name
*
* @return the Account Holder's first name
*/
public String getFirstName()
{
return name.getFirst();
}
/**
* Get the Account Holder's last name
*
* @return the Account Holder's last name
*/
public String getLastName()
{
return name.getLast();
}
/**
* Get the Account Holder's account Number
*
* @return the Account Holder's account number
*/
public String getAccountNumber()
{
return accountNumber;
}
public void printAccountDetails()
{
System.out.println( toString());
}
/**
* Return details of an account as a formated string
*
* @return the account details of a particular account
*/
public String toString()
{
String output = accountNumber + " ";
output = output + name.toString() + "\n";
return output;
}
// mutators
/**
* Change the first name
*
* @param fName the new first name
*
*/
public void setFirstName(String fName)
{
name.setFirst (fName);
}
/**
* Change the last name
*
* @param lName the new last name
*
*/
public void setLastName(String lName)
{
name.setLast(lName);
}
} // end Account class
GUI类中的相关代码
/**
* Write a description of class HW4GUI here.
*
* @author (your name)
* @version (a version number or a date)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HW4GUI extends JFrame implements ActionListener
{
private BasicAccountList accounts;
private JPanel buttonPanel;
private JButton jbtAdd;
private JButton jbtNumber;
private JButton jbtQuit;
private JLabel jlbAcctNo;
private JLabel jlbFName;
private JLabel jlbLName;
private JTextField jtfAcctNo;
private JTextField jtfFName;
private JTextField jtfLName;
private int nextAcctNo;
private JPanel textPanel;
public HW4GUI ()
{
makeFrame();
showFrame();
nextAcctNo = 1001;
}
public void actionPerformed(ActionEvent ae)
{
BasicAccountList accountlist = new BasicAccountList ();
String item = ae.getActionCommand();
String firstNameText = jtfFName.getText();
String lastNameText = jtfLName.getText();
String finalAccountNumber = jtfAcctNo.getText();
if(item.equals("No. of Accounts"))
{
jbtAdd.setEnabled(false);
jbtNumber.setText ("Clear");
jlbAcctNo.setText("No. of accounts:");
//accounts.getNumberOfAccounts();
BasicAccount newaccount = new BasicAccount(firstNameText, lastNameText, finalAccountNumber);
String accountTotal = Integer.toString (accountlist.getNumberOfAccounts());
jtfAcctNo.setText (accountTotal);
}
}
答案 0 :(得分:1)
您在BasicAccountList
方法中创建了另一个actionPerformed
。这意味着,每次单击按钮时,都会生成一个新的BasicAccountList
并执行此列表中的所有操作,而不是HW4GUI
所持有的操作。
答案 1 :(得分:0)
不是在实现ActionListener
的类中实现JFrame
,而是在单独的类中执行它(可能在匿名类中,但任何类都没问题)会更好。
现在你可以有两个单独的类实现ActionListener
,因此有两个单独的actionPerformed
实现,每个按钮一个。
将这些ActionListener
附加到相应的按钮上,你应该很高兴。
注意:帐户列表应该是框架的成员,因此您可以跨ActionListener
分享。