java - 使用 JavaFX 为 ATM 构建 GUI,但在主阶段更改场景时遇到困难

标签 java user-interface javafx scene stage

尝试使用事件处理程序来同步特定的按钮按下,以前进到 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);
  }
}

最佳答案

我认为删除 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/

关于java - 使用 JavaFX 为 ATM 构建 GUI,但在主阶段更改场景时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31598391/

相关文章:

javafx - 从最小化 WebView 恢复后在 javafx 中被禁用

JavaFX ActionEvent 矩形

java - java中如何在特定时间调用一个方法?

java - 将行添加到数据模型时出现 ArrayIndexOutOfBoundsException

java - 请建议我使用 GridBag 布局之外的基本布局

java - 单击按钮以相同的形式打开文本 Pane

没有 Prism 的 WPF UI 组合

java - 如何解决 android.os.NetworkOnMainThreadException?

java - 在 spring bean 中自动装箱

JavaFX Css 上下文菜单不工作