java - How can I keep handlers for different buttons in different classes in JFrame? -
i have problem program using jframe. making "cash machine" program, asks user first name, last name, current account state , withdrawal amount. want have 2 classes implements 2 different tasks program does. class card should ask user data said before, , after clicking "accept" button should give message box information "hello [user], have withdrawed [amount], current account state [amount]." if user exceeds "zero state" means wants withdraw more has, program pops message box decline information. second class creditcard same, allows user make debt 1500. have 2 handlers : 1 card works fine after clicking "accept" button , second 1 creditcard doesn't work @ all. know problem proper inheritance, can't solve it. important me store creditcard handler in creditcard class (if it's possible of course).
my code below: card class:
public class card extends jframe { public jtextfield firstname; public jtextfield lastname; public jtextfield state; public jtextfield withdrawal; private jbutton accept; public jbutton credit_card; private jlabel firstnamelabel; public card() { super("cash machine"); setlayout(new flowlayout()); firstnamelabel = new jlabel("first name"); add(firstnamelabel); firstname = new jtextfield("first name"); add(firstname); lastname = new jtextfield("last name"); add(lastname); state = new jtextfield("current account state"); add(state); withdrawal = new jtextfield("amount of withdrawal"); add(withdrawal); accept = new jbutton("accept"); add(accept); credit_card = new jbutton("credit card"); add(credit_card); handler1 handler = new handler1(); accept.addactionlistener(handler); } private class handler1 implements actionlistener { public void actionperformed(actionevent event) { string state1 = state.gettext(); int state2 = integer.parseint(state1); string withdrawal1 = withdrawal.gettext(); int withdrawal2 = integer.parseint(withdrawal1); int finalstate = state2 - withdrawal2; // showing final message box if(event.getsource()==accept) if(finalstate > 0) joptionpane.showmessagedialog(null, "hello " + firstname.gettext() + " " + lastname.gettext() + " .your current account state is: " + finalstate); else joptionpane.showmessagedialog(null,"you out of money on debit account"); } } }
creditcard class:
public class creditcard extends card { public creditcard(){ handler1 handler = new handler1(); credit_card.addactionlistener(handler); } private class handler1 implements actionlistener { public void actionperformed(actionevent event) { string state1 = state.gettext(); int state2 = integer.parseint(state1); string withdrawal1 = withdrawal.gettext(); int withdrawal2 = integer.parseint(withdrawal1); int finalstate = state2 - withdrawal2; if(event.getsource()==credit_card) if(finalstate >= -1500) joptionpane.showmessagedialog(null, "hello " + firstname.gettext() + " " + lastname.gettext() + " .your current account state is: " + finalstate); else joptionpane.showmessagedialog(null,"your credit card limit has been reached"); } }
}
to me seems code working fine. maybe forgot instantiate creditcard instead of card?
Comments
Post a Comment