事件处理程序和setOnAction?

huangapple go评论64阅读模式
英文:

Event Handlers and setOnAction?

问题

Main.java:

public class Main extends Application {

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

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

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;
    }
}
英文:

so I am new to JavaFX and basically I designed a button in a separate class called interfaceLayout I call these methods in the main class and placed those nodes into a wrapper class HBox, now the issue I am having is adding action to my buttons? I want to be able to call the buttons from the interfaceLayout class and add a action to them from Main, and then add a function to that action which is to open up a new scene. Here is my code:

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

答案1

得分: 0

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

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

}
英文:

You basically just need to create a reference to each button you create. Use that reference to create action handlers for the buttons.

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

huangapple
  • 本文由 发表于 2020年10月11日 07:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64299133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定