【问题标题】:Event Handlers and setOnAction?事件处理程序和 setOnAction?
【发布时间】:2020-10-10 23:06:55
【问题描述】:

所以我是 JavaFX 的新手,基本上我在一个名为 interfaceLayout 的单独类中设计了一个按钮,我在主类中调用这些方法并将这些节点放入包装类 HBox,现在我遇到的问题是向我的按钮添加操作?我希望能够从 interfaceLayout 类调用按钮并从 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;
    }
}

【问题讨论】:

  • 制作按钮的实例变量。然后使用 getter 获取按钮。
  • 为按钮创建了实例变量,但我不确定如何为它们设置样式?当我尝试为它们设置样式时,我只是得到错误?
  • 再次查看您的代码后,最好使用静态方法。
  • 请使用 java 命名约定 ...并完成有关 java 语言基础的教程 ...您必须了解在调用 createButton 时您在做什么:它名副其实 - 返回一个 每次调用的新按钮 :) 相反,在第一次调用时保留一个引用并在该实例上设置操作。
  • @Sedrick 建议 static 是一个次优的想法:在这里可能没问题,仅当 InterfaceLayout 旨在成为一种实用程序工厂类。但不能与您的第一条评论结合使用。

标签: java button javafx action


【解决方案1】:

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

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();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2018-01-04
    • 2015-04-05
    相关资源
    最近更新 更多