如何获取对象的名称并将其显示在JFrame的标题中

时间:2017-01-12 06:48:35

标签: java swing oop arraylist jframe

我想在我的ArrayList中获取我的第一个Object的名称,但我真的不知道我是怎么做到的。我这个名字应该是我的JFrame的标题。

这是我的JFrame:

public class TransactionFrame extends JFrame implements ActionListener {
private Transaction trans;
private Bank bank;
private Customer customer;
private Account acc;
private Toolkit t;
private int x=0, y=0, width=800, height = 600;

public TransaktionFrame(Konto konto) throws HeadlessException {
    super();
    this.konto = konto;
    t = Toolkit.getDefaultToolkit();
    Dimension d = t.getScreenSize();
    x = (int)((d.getWidth() - width) / 2);
    y = (int)((d.getHeight()- height)/ 2);

    setTitle("Application Java" );

    setBounds(x, y, width, height);
    setVisible(true);
}
}

下一个是我的类Menue,其方法名为InitOfObjects

public class Menue {

private Bank bank = new Bank();
private Account account = new Account();
private Customer customer= new Customer();

   public void initOfObjects(){
    Customer customer1 = new Privatecustomer("Ragnar", "Lothbrok", "017634456657", "Lothbrok@ragnar.de", "15.07.1979",
    new Adress("Ragnarstraße", "22", "12159", "Berlin"), "2");
    bank.addCustomer(customer1);

    Kunde customer2 = new Companycustomer("Varian", "Lothbrok", "017634456657", "Lothbrok@ragnar.de", "15.07.1979",
    new Adress("Ragnarstraße", "22", "12159", "Berlin"), "2");
    bank.addCustomer(customer2);

    Customer customer3 = new Privatecustomer("Rengar", "Lothbrok", "017634456657", "Lothbrok@ragnar.de", "15.07.1979",
    new Adress("Ragnarstraße", "22", "12159", "Berlin"), "2");
    bank.addCustomer(customer3);
 }
}

我的银行班:

public class Bank {

private String name;
private String bic;
private Adress adress;
private ArrayList<Customer> customerList;

public Bank() {
    super();
    this. customerList = new ArrayList<Customer>();
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getbIC() {
    return bic;
}// end of getbIC()

public void setbIC(String bic) {
    this.bic = bic;
}

public Adress getAdress() {
    return adress;
}

public void setAdress(Adress adress) {
    this.adress = adress;
}

public int sizeOfArraylist(){
    return customerList.size();
}

public Customer get(int index){
    return customerList.get(index);

}

public ArrayList<Customer> getCustomerList() {
    return customerList;
}// end of getCustomerList()

public void setCustomerList(ArrayList<Customer> customerList) {
    this. customerList = customerList;
}

public boolean addAccount(Account account) {
    for (Customer c : customerList) {
        if (c.getCustomerNumber().equals(account.getCustomerNb())) {
            boolean sucess = k.addAccount(account);
            return sucess;
        }
    }
    return false;
}

public void addCustomer(Customer customer) {
    this.customerList.add(customer);
}

@Override
public String toString() {
    return "Bank [Name= " + name + ", BIC= " + bic + ", Adress= " + adress + ", Customer= " + customerList + " ]";
}

}

我的客户类:

public class Customer implements Comparator<Customer> {

protected String customerNumber;
private Adress adress;
protected ArrayList<Account> account;


public Customer(String cnb, Adress adress) {
    super();
    this.customerNumber = cnb;
    this.adress = adress;
}

public Customer(String customerNumber) {
    this.customerNumber = customerNumber;
}

public Customer() {

}

public Adress getAdress() {
    return adress;
}

public void setAdress(Adress adress) {
    this.adress = adress;
}

public String getCustomerNumber() {
    return customerNumber;
}

public Customer setCustomerNumber(String customerNumber) {
    this.customerNumber = customerNumber;
    return this;
}

public ArrayList<Account> getAccount() {
    return account;
}


public boolean addAccount(Account acc) {
    if (account == null)
        account = new ArrayList<Account>();


    if (account.size() > 10)
        return false; 


    account.add(k);

    return true;
}


@Override
public int compare(Customer o1, Customer o2) {

    return o1.customerNumber.compareTo(o2.customerNumber);
}

}

我的私人客户类(相关简称):

public class Privatecustomer extends Customer {

private String firstname;
private String lastname;
private Adresse adress;
private String customerNumber;

public Privatkunde(String firstname, String lastname, Adress adress, String kndnr) {
    super(kndnr, adress);
    this.firstname = firstname;
    this.lastname = lastname;
    this.adresse = adress;
    this.customerNumber = cnb;

} 
public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname () {
    return lastname;
}

public void setLastname (String lastname) {
    this.lastname = lastname;
}

public Adress getAdress() {
    return adress;
}

public void setAdress(Adress adress) {
    this.adress = adress;
}

}

我的公司客户类别(相关简称):

public class Companycustomer extends Customer {


private String companyName;
private Adresse adr;

public Companycustomer(String companyName, Adress adress, String customerNumber) {
    super(customerNumber, adress);
    this.companyName = firmenName;
    this.adr = adress;

}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public Adresse getAdr() {
    return adr;
}

public void setAdr(Adress adr) {
    this.adr = adr;
}

私人客户和公司客户来自客户。 我只是想说代码实际上有用。 我只想知道如何取出客户的姓名和公司客户名称。 为什么我使用Swing,因为老师说的。我想学习javafx,但老师说没有。 如果您需要更多代码,请写信给我。我是Java的新手。

最诚挚的问候 Maskulin

2 个答案:

答案 0 :(得分:0)

下面是伪代码,

Menue  menu = new Menue();
menu.initOfObjects();//populate list of customers

Bank bankObj=menu.getBank();
List<Customer> customers=bankObj.getCustomers();//get List of customers

if(customers!=null && customers.size()>0)
setTitle(customers.get(0).getCustomerName() );//get First customer details

答案 1 :(得分:0)

我不太确定你的完整问题,但我想我知道你想要什么,所以我会用一些想象力来回答你。

我假设您正在谈论的ArrayList位于 Bank 类的字段库中,并且您通过 Menue 类存储所有帐户,因此第一步是在两个类中创建一个getter:

public class Bank {

    ...

    ArrayListz<Customer> list;

    public int size()
    {
        return list.size();
    }

    // Notice that this WILL ASSUME that index is < than size
    public Customer get(int index)
    {
        return list.get(index);
    }
}

public class Menue {

...

    public Customer getCustomer(int index)
    {
        // If element exists at 'index', return it. If it doesn't, return null
        return index >= bank.size() ? null : bank.get(index);
    }

}

您需要客户字段值,因此您需要执行类似上面但在正确类中的操作。现在,您可以访问所需的所有信息,因此假设您在JFrame类中正确初始化了 bank 字段,您可以执行以下操作:

public class TransactionFrame extends JFrame implements ActionListener {

    private Bank bank;
    ...

    public TransaktionFrame(Konto konto) throws HeadlessException {
        super();
        ...

        //this will return the name of the first customer    
        Customer customer = bank.getCustomer(0);
        String name = customer == null ? "" : customer.getName();

        ...

        setTitle(name); //this will use the name of your the first customer as the title of the JFrame

    }
}

我不确定这是否可以解决您的问题,但我希望它有所帮助:)