Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取对象的名称并将其显示在JFrame的标题中_Java_Swing_Oop_Arraylist_Jframe - Fatal编程技术网

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

Java 如何获取对象的名称并将其显示在JFrame的标题中,java,swing,oop,arraylist,jframe,Java,Swing,Oop,Arraylist,Jframe,我想在我的ArrayList中取我的第一个对象的名称,但我真的不知道我是如何做到这一点的。我取的这个名字应该是我JFrame的标题 这是我的JFrame: public class TransactionFrame extends JFrame implements ActionListener { private Transaction trans; private Bank bank; private Customer customer; private Account acc; priva

我想在我的ArrayList中取我的第一个对象的名称,但我真的不知道我是如何做到这一点的。我取的这个名字应该是我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 + " ]";
}

}
我的公司客户类别(缩写为相关):

Privatecustomer和Companycustomer从Customer扩展而来。 我只想说代码实际上是有效的。 我只是想知道我怎样才能把客户的名字和姓氏以及公司客户的名字去掉。 我为什么用秋千,因为老师说了。我真的很想学javafx,但是老师拒绝了。 如果你需要更多的代码,请给我写信。我对Java真的很陌生

致意
下面的Maskulin是相同的伪代码

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
Menue菜单=新建菜单();
menu.initOfObjects()//填充客户列表
Bank bankObj=menu.getBank();
List customers=bankObj.getCustomers()//获取客户列表
if(customers!=null&&customers.size()>0)
setTitle(customers.get(0.getCustomerName())//获取第一个客户的详细信息

我不太清楚你的全部问题,但我想我知道你想要什么,所以我会用一些想象力来回答你

我将假设您所讨论的ArrayList位于类bankbank字段中,并且您通过类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);
    }

}

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

想给您一个好建议,尝试学习一些java fx。。。swing是如此的糟糕,发布你的
银行
类代码。你的菜单类需要某种方式与框架对话,虽然有些人可能建议传递框架的引用,但我会创建一个接口,描述其他类可以/可以做什么,并实现
setBounds(x,y,width,height)最好是
pack()
使帧达到所需大小,而不使用幻数猜测,然后是
setLocationRelativeTo(null)把它放在屏幕的中间。@ X战斗机……Swing太糟糕了。新手在判断GUI工具包的质量方面很差。如果愿意的话,建议从稳定、健壮的Swing更新,但不要说它的坏话,因为这只是强调你从未学会如何使用它。我只是编辑我的帖子,因为我在Customer类中没有Customer的名称。这是公司客户和私人客户的课程。我刚刚编辑了我的课程。我理解你的解决方法,但客户没有名字。Name位于Privatecustomer和Companycustomer类上。另外,我在Customer Customer=bank.getCustomer(0);(行)中的TransactionFrame类上得到一个Nullpointerexception;。
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;
}
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
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);
    }

}
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

    }
}