简体   繁体   中英

How can I use JTextField input as the identifier of a new class instance?

I have a UserInput class which contains a JTextField that accepts a users name. I also have a Paycheck class. I would like to create a new instance of Paycheck using the text of the JTextField as the identifier. Eg If the user types Troy, I would like to be able to create a new instance:

 Paycheck Troy = new Paycheck();  

I am currently recieving an incompatable type error.

What about adding the user name to a field within the Paycheck class? This way you will be able to identify the name that has been entered. You normally are not able to set names of variables this way.

You could store the Paychecks in a Map:

JTextField name; // text = "Troy";
Map<String, Paycheck> paychecks = new HashMap<String, Paycheck>();

paychecks.put(name.getText(), new Paycheck());

then retrieve it with:

Paycheck paycheck = paychecks.get("Troy");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM