【问题标题】:Using JavaFX to build a GUI for an ATM but having trouble getting primary stage to change scene使用 JavaFX 为 ATM 构建 GUI,但无法在初级阶段更改场景
【发布时间】:2015-07-23 21:30:51
【问题描述】:

尝试使用事件处理程序来同步特定的按钮按下以前进到 ATM 上的下一个“屏幕”,方法是:隐藏舞台,使用按钮按下创建的场景更新舞台,然后重新显示舞台。

我很好奇这个过程是否只能如此深入,因为我的 newCheckingsAccounts 按钮没有做任何应该做的事情,但我可以在那个页面上倒退,我或多或少地使用相同的代码来尝试保持继续前进。

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 that will store the 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 Back to Main Menu");
Button btNewAccount = new Button("Make New Account");
Button btExistingAccount = new Button("Access an Existing Account");
Button btNewCheckings = new Button("Make New Checkings");
Button btNewSavings = new Button("Make New Savings");

@Override // Override the start method in the Application class
  public void start(Stage primaryStage) {

// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btGoToAccounts = new Button("Go To 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 a scene and place it in the stage
Scene scene = new Scene(borderPane, 500, 300);
primaryStage.setTitle("Bank of America"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage

// creates and registers handler and specifies action for button to go to accounts page
btGoToAccounts.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        primaryStage.hide();

        // Hold three buttons in an 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 the stage title
        primaryStage.setScene(scene1); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }
}); 

// creates and registers handler and specifies action for button to go to create new accounts page
btNewAccount.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        primaryStage.hide();

        // Hold three buttons in an 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 the stage title
        primaryStage.setScene(scene2); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }
});

// THIS IS THE BUTTON THAT DOESN'T REGISTER AS BEING CLICKED...havent done the newSavingsAccount button either.want it to take me to new scene where i enter in new account info hit submit and then take me back to the 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 the stage title
        primaryStage.setScene(scene3); // Place the scene in the stage

        //the Name text field
        final TextField name = new TextField();
        name.setPromptText("Enter the desired account name which will be used under the access an existing account screen later");
        name.setPrefColumnCount(10);
        name.getText();
        hbox3.getChildren().add(name);

        //Defining the initial balance/fee text fields
        final TextField initialBalance = new TextField();
        final TextField fee = new TextField();
        initialBalance.setPromptText("Enter your desired initial balance as a double.");
        fee.setPromptText("Enter the agreed upon fee per transaction as a double.");

        initialBalance.setPrefColumnCount(15);
        fee.setPrefColumnCount(15);
        fee.getText();
        hbox3.getChildren().add(fee);
        initialBalance.getText();
        hbox3.getChildren().add(initialBalance);

        //Defining the Submit button
        Button accountCreation = new Button("Create the Account");
        hbox3.getChildren().add(accountCreation);

        //Defining the Clear button
        Button clear = new Button("Clear");
        hbox3.getChildren().add(clear);

        primaryStage.show(); // Display the stage

        //Setting an action for the 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 an action for the Clear button
        clear.setOnAction(new EventHandler<ActionEvent>() {

        @Override
            public void handle(ActionEvent e) {
                name.clear();
                initialBalance.clear();
            }
        });

    }
});

// creates and registers handler and specifies action for end button to close the stage
btEnd.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        primaryStage.close();
    }
});


// creates and registers handler and specifies action for main menu button to go to first scene
btMain.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        primaryStage.hide();
        primaryStage.setTitle("Bank of America"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }
});

  }

   /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
   */

  public static void main(String[] args) {
launch(args);
  }
}

【问题讨论】:

    标签: java user-interface javafx scene stage


    【解决方案1】:

    我认为删除 btNewAccount.setOnAction() 中两个按钮的覆盖应该可以解决您的问题:

    // creates and registers handler and specifies action for button to go
        // to create new accounts page
        btNewAccount.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                primaryStage.hide();
    
                // Hold three buttons in an HBox
                HBox hBox2 = new HBox();
                hBox2.setSpacing(10);
                hBox2.setAlignment(Pos.CENTER);
    
                // ############### removed this ###################
                //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 the stage title
    
                primaryStage.setScene(scene2); // Place the scene in the stage
                primaryStage.show(); // Display the stage
            }
        });
    

    如果你想在一个Button的ActionHandler中实例化,你需要在同一个Block中声明它的ActionHandler。

    您的操作方式(start 方法中的 ActionHandler)只有在您使用您在顶部初始化的类属性(按钮)时才会起作用。

    希望我能帮上忙:)

    编辑:对于这个项目来说,一个好的提示可能是使用 JavaFX 查看 FXML 文件。这可能会对您有所帮助:http://code.makery.ch/library/javafx-8-tutorial/part1/

    【讨论】:

    • 谢谢没听懂!
    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    相关资源
    最近更新 更多