java - Using JavaFX to build a GUI for an ATM but having trouble getting primary stage to change scene -
trying use event handlers sync specific button pushes advance next "screen" on atm by: hiding stage, updating stage scene button push creates, , reshowing stage.
i curious if process can taken deep since button newcheckingsaccounts isn't doing should, can go backwards on page , used same code more or less try keep going forward.
import java.awt.insets; import java.util.arraylist; import java.util.scanner; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.textfield; import javafx.scene.layout.gridpane; import javafx.scene.layout.stackpane; import javafx.scene.layout.hbox; import javafx.scene.layout.borderpane; import javafx.scene.paint.color; import javafx.scene.shape.circle; import javafx.stage.stage; public class testaccount extends application { //creates arrays store accounts arraylist<account> checking = new arraylist<account>(); arraylist<integer> savings = new arraylist<integer>(); //declares variables double interest = 0; double interestrate = 0; double balance = 0; double credit = 0; double initialbalance = 0; double feechargedpertransaction = 0; button btmain = new button("go main menu"); button btnewaccount = new button("make new account"); button btexistingaccount = new button("access existing account"); button btnewcheckings = new button("make new checkings"); button btnewsavings = new button("make new savings"); @override // override start method in application class public void start(stage primarystage) { // hold 2 buttons in hbox hbox hbox = new hbox(); hbox.setspacing(10); hbox.setalignment(pos.center); button btgotoaccounts = new button("go accounts page"); button btend = new button("end program"); hbox.getchildren().add(btgotoaccounts); hbox.getchildren().add(btend); borderpane borderpane = new borderpane(); borderpane.setbottom(hbox); borderpane.setalignment(hbox, pos.center); // create scene , place in stage scene scene = new scene(borderpane, 500, 300); primarystage.settitle("bank of america"); // set stage title primarystage.setscene(scene); // place scene in stage primarystage.show(); // display stage // creates , registers handler , specifies action button go accounts page btgotoaccounts.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { primarystage.hide(); // hold 3 buttons in hbox hbox hbox1 = new hbox(); hbox1.setspacing(10); hbox1.setalignment(pos.center); hbox1.getchildren().add(btnewaccount); hbox1.getchildren().add(btexistingaccount); hbox1.getchildren().add(btmain); borderpane borderpane1 = new borderpane(); borderpane1.setbottom(hbox1); borderpane.setalignment(hbox1, pos.center); scene scene1 = new scene(borderpane1, 500, 300); primarystage.settitle("accounts page"); // set stage title primarystage.setscene(scene1); // place scene in stage primarystage.show(); // display stage } }); // creates , registers handler , specifies action button go create new accounts page btnewaccount.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { primarystage.hide(); // hold 3 buttons in hbox hbox hbox2 = new hbox(); hbox2.setspacing(10); hbox2.setalignment(pos.center); button btnewcheckings = new button("make new checkings"); button btnewsavings = new button("make new savings"); hbox2.getchildren().add(btnewcheckings); hbox2.getchildren().add(btnewsavings); hbox2.getchildren().add(btmain); borderpane borderpane2 = new borderpane(); borderpane2.setbottom(hbox2); borderpane.setalignment(hbox2, pos.center); scene scene2 = new scene(borderpane2, 800, 300); primarystage.settitle("new accounts"); // set stage title primarystage.setscene(scene2); // place scene in stage primarystage.show(); // display stage } }); // button doesn't register being clicked...havent done newsavingsaccount button either.want take me new scene enter in new account info hit submit , take me main menu ("scene") btnewcheckings.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { hbox hbox3 = new hbox(); scene scene3 = new scene(hbox3, 800, 300); primarystage.settitle("test"); // set stage title primarystage.setscene(scene3); // place scene in stage //the name text field final textfield name = new textfield(); name.setprompttext("enter desired account name used under access existing account screen later"); name.setprefcolumncount(10); name.gettext(); hbox3.getchildren().add(name); //defining initial balance/fee text fields final textfield initialbalance = new textfield(); final textfield fee = new textfield(); initialbalance.setprompttext("enter desired initial balance double."); fee.setprompttext("enter agreed upon fee per transaction double."); initialbalance.setprefcolumncount(15); fee.setprefcolumncount(15); fee.gettext(); hbox3.getchildren().add(fee); initialbalance.gettext(); hbox3.getchildren().add(initialbalance); //defining submit button button accountcreation = new button("create account"); hbox3.getchildren().add(accountcreation); //defining clear button button clear = new button("clear"); hbox3.getchildren().add(clear); primarystage.show(); // display stage //setting action submit button accountcreation.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { if ((initialbalance.gettext() != null && !initialbalance.gettext().isempty())) { checkingaccount newmember = new checkingaccount(); newmember.setinitialbalance(double.parsedouble(initialbalance.tostring())); newmember.setfee((double.parsedouble(fee.tostring()))); checking.add(newmember); } else { system.out.println("no member added"); } } }); //setting action clear button clear.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { name.clear(); initialbalance.clear(); } }); } }); // creates , registers handler , specifies action end button close stage btend.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { primarystage.close(); } }); // creates , registers handler , specifies action main menu button go first scene btmain.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { primarystage.hide(); primarystage.settitle("bank of america"); // set stage title primarystage.setscene(scene); // place scene in stage primarystage.show(); // display stage } }); } /** * main method needed ide limited * javafx support. not needed running command line. */ public static void main(string[] args) { launch(args); } }
i think removing overriding of both buttons in btnewaccount.setonaction() should solve problem:
// creates , registers handler , specifies action button go // create new accounts page btnewaccount.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { primarystage.hide(); // hold 3 buttons in hbox hbox hbox2 = new hbox(); hbox2.setspacing(10); hbox2.setalignment(pos.center); // ############### removed ################### //button btnewcheckings = new button("make new checkings"); //button btnewsavings = new button("make new savings"); hbox2.getchildren().add(btnewcheckings); hbox2.getchildren().add(btnewsavings); hbox2.getchildren().add(btmain); borderpane borderpane2 = new borderpane(); borderpane2.setbottom(hbox2); borderpane.setalignment(hbox2, pos.center); scene scene2 = new scene(borderpane2, 400, 300); primarystage.settitle("new accounts"); // set stage title primarystage.setscene(scene2); // place scene in stage primarystage.show(); // display stage } });
if want instantiate in actionhandler of button, need declare it's actionhandler in same block.
the way did (actionhandler in start method), work, if use class atributes (buttons) have initialized @ top.
hope :)
edit: tip project looking @ fxml files javafx. might you: http://code.makery.ch/library/javafx-8-tutorial/part1/
Comments
Post a Comment