事件处理程序和 setOnAction?

Event Handlers and setOnAction?

所以我是 JavaFX 的新手,基本上我在一个单独的 class 中设计了一个按钮,称为 interfaceLayout 我在主 class 中调用这些方法并将这些节点放入包装器中class HBox,现在我遇到的问题是向我的按钮添加操作?我希望能够从 interfaceLayout class 调用按钮并从 Main 向它们添加一个动作,然后向该动作添加一个功能,即打开一个新场景。这是我的代码:

Main.java

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        //Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        interfaceLayout ui = new interfaceLayout();
        BorderPane root = new BorderPane();


        HBox topHb = new HBox(ui.createBtn(),ui.dashboardBtn());
        topHb.setSpacing(10);
        root.setTop(topHb);
        topHb.setStyle("-fx-background-color: #34475A;" +
                "-fx-set-spacing: 10;" +
                "-fx-padding: 10;");

        HBox leftHb = new HBox(ui.currentProjects());
        leftHb.setSpacing(10);
        root.setLeft(leftHb);
        leftHb.setStyle("-fx-background-color: #34475A; -fx-set-spacing: 10; -fx-padding: 10;");
        
        primaryStage.setTitle("Project Management");
        primaryStage.setScene(new Scene(root, 1000, 600));
        primaryStage.show();


        //ui.createBtn().setOnAction(e -> form );
        //btn.addEventHandler(ActionEvent.ACTION, (e) -> );

    }

interfaceLayout.java

import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

public class interfaceLayout {
    public ListView currentProjects () {

        ListView list = new ListView();
        String test = "Hey";
        list.getItems().add(test);

        list.setStyle ("-fx-background-color: #283B4E;" +
                "-fx-min-width: 200px; " +
                "-fx-min-height: 450px; " +
                "-fx-max-width: 200px; " +
                "-fx-max-height: 450px;");
        return list;
    }

    public Button createBtn() {
        Button btn = new Button();

        btn.setText("Create a Project");
        btn.setTextFill(Color.WHITE);
        btn.setFont(Font.font("Times New Roman", FontWeight.BOLD, 15));
        btn.setStyle ("-fx-background-color: #283B4E;" + "-fx-border-radius: 500;" + "-fx-background-radius: 8em; " +
                "-fx-min-width: 150px; " +
                "-fx-min-height: 60px; " +
                "-fx-max-width: 150px; " +
                "-fx-max-height: 60px;" +
                "-fx-effect: dropShadow(gaussian, rgba(0, 0, 0, 0.3), 15, 0.5, 0.5, 0);");

        return btn;
    }

    public Button dashboardBtn() {

        Button btn = new Button();
        btn.setMinWidth(150);
        btn.setMinHeight(70);
        btn.setText("Dashboard");
        btn.setTextFill(Color.WHITE);
        btn.setFont(Font.font("Times New Roman", FontWeight.BOLD, 15));
        btn.setStyle ("-fx-background-color: #283B4E;" + "-fx-background-radius: 8em; " +
                "-fx-min-width: 150px; " +
                "-fx-min-height: 60px; " +
                "-fx-max-width: 150px; " +
                "-fx-max-height: 60px;" +
                "-fx-effect: dropShadow(gaussian, rgba(0, 0, 0, 0.3), 15, 0.5, 0.5, 0);");

        return btn;
    }
}

您基本上只需要为您创建的每个按钮创建一个引用。使用该引用为按钮创建操作处理程序。

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class App extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        interfaceLayout ui = new interfaceLayout();
        BorderPane root = new BorderPane();

        Button createBtnOne = ui.createBtn();
        createBtnOne.setOnAction((t) -> {
            System.out.println("You pressed creatBtnOne");
        });        
        Button dashboardBtnOne = ui.dashboardBtn();
        dashboardBtnOne.setOnAction((t) -> {
            System.out.println("You pressed dashboardBtnOne");
        });        
        HBox topHb = new HBox(createBtnOne, dashboardBtnOne);
        topHb.setSpacing(10);
        root.setTop(topHb);
        topHb.setStyle("-fx-background-color: #34475A;" +
                "-fx-set-spacing: 10;" +
                "-fx-padding: 10;");

        HBox leftHb = new HBox(ui.currentProjects());
        leftHb.setSpacing(10);
        root.setLeft(leftHb);
        leftHb.setStyle("-fx-background-color: #34475A; -fx-set-spacing: 10; -fx-padding: 10;");
        
        primaryStage.setTitle("Project Management");
        primaryStage.setScene(new Scene(root, 1000, 600));
        primaryStage.show();
    }

}