事件处理程序和setOnAction?

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

Event Handlers and setOnAction?

问题

  1. Main.java:
  2. public class Main extends Application {
  3. @Override
  4. public void start(Stage primaryStage) throws Exception{
  5. interfaceLayout ui = new interfaceLayout();
  6. BorderPane root = new BorderPane();
  7. HBox topHb = new HBox(ui.createBtn(), ui.dashboardBtn());
  8. topHb.setSpacing(10);
  9. root.setTop(topHb);
  10. topHb.setStyle("-fx-background-color: #34475A;" +
  11. "-fx-set-spacing: 10;" +
  12. "-fx-padding: 10;");
  13. HBox leftHb = new HBox(ui.currentProjects());
  14. leftHb.setSpacing(10);
  15. root.setLeft(leftHb);
  16. leftHb.setStyle("-fx-background-color: #34475A; -fx-set-spacing: 10; -fx-padding: 10;");
  17. primaryStage.setTitle("Project Management");
  18. primaryStage.setScene(new Scene(root, 1000, 600));
  19. primaryStage.show();
  20. }
  21. }
  22. interfaceLayout.java:
  23. import javafx.event.ActionEvent;
  24. import javafx.scene.Scene;
  25. import javafx.scene.control.Button;
  26. import javafx.scene.control.ListView;
  27. import javafx.scene.paint.Color;
  28. import javafx.scene.text.Font;
  29. import javafx.scene.text.FontWeight;
  30. public class interfaceLayout {
  31. public ListView currentProjects () {
  32. ListView list = new ListView();
  33. String test = "Hey";
  34. list.getItems().add(test);
  35. list.setStyle("-fx-background-color: #283B4E;" +
  36. "-fx-min-width: 200px; " +
  37. "-fx-min-height: 450px; " +
  38. "-fx-max-width: 200px; " +
  39. "-fx-max-height: 450px;");
  40. return list;
  41. }
  42. public Button createBtn() {
  43. Button btn = new Button();
  44. btn.setText("Create a Project");
  45. btn.setTextFill(Color.WHITE);
  46. btn.setFont(Font.font("Times New Roman", FontWeight.BOLD, 15));
  47. btn.setStyle("-fx-background-color: #283B4E;" +
  48. "-fx-border-radius: 500;" +
  49. "-fx-background-radius: 8em; " +
  50. "-fx-min-width: 150px; " +
  51. "-fx-min-height: 60px; " +
  52. "-fx-max-width: 150px; " +
  53. "-fx-max-height: 60px;" +
  54. "-fx-effect: dropShadow(gaussian, rgba(0, 0, 0, 0.3), 15, 0.5, 0.5, 0);");
  55. return btn;
  56. }
  57. public Button dashboardBtn() {
  58. Button btn = new Button();
  59. btn.setMinWidth(150);
  60. btn.setMinHeight(70);
  61. btn.setText("Dashboard");
  62. btn.setTextFill(Color.WHITE);
  63. btn.setFont(Font.font("Times New Roman", FontWeight.BOLD, 15));
  64. btn.setStyle("-fx-background-color: #283B4E;" + "-fx-background-radius: 8em; " +
  65. "-fx-min-width: 150px; " +
  66. "-fx-min-height: 60px; " +
  67. "-fx-max-width: 150px; " +
  68. "-fx-max-height: 60px;" +
  69. "-fx-effect: dropShadow(gaussian, rgba(0, 0, 0, 0.3), 15, 0.5, 0.5, 0);");
  70. return btn;
  71. }
  72. }
英文:

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

  1. public class Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) throws Exception{
  4. //Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
  5. interfaceLayout ui = new interfaceLayout();
  6. BorderPane root = new BorderPane();
  7. HBox topHb = new HBox(ui.createBtn(),ui.dashboardBtn());
  8. topHb.setSpacing(10);
  9. root.setTop(topHb);
  10. topHb.setStyle("-fx-background-color: #34475A;" +
  11. "-fx-set-spacing: 10;" +
  12. "-fx-padding: 10;");
  13. HBox leftHb = new HBox(ui.currentProjects());
  14. leftHb.setSpacing(10);
  15. root.setLeft(leftHb);
  16. leftHb.setStyle("-fx-background-color: #34475A; -fx-set-spacing: 10; -fx-padding: 10;");
  17. primaryStage.setTitle("Project Management");
  18. primaryStage.setScene(new Scene(root, 1000, 600));
  19. primaryStage.show();
  20. //ui.createBtn().setOnAction(e -> form );
  21. //btn.addEventHandler(ActionEvent.ACTION, (e) -> );
  22. }

interfaceLayout.java

  1. import javafx.event.ActionEvent;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.control.ListView;
  5. import javafx.scene.paint.Color;
  6. import javafx.scene.text.Font;
  7. import javafx.scene.text.FontWeight;
  8. public class interfaceLayout {
  9. public ListView currentProjects () {
  10. ListView list = new ListView();
  11. String test = "Hey";
  12. list.getItems().add(test);
  13. list.setStyle ("-fx-background-color: #283B4E;" +
  14. "-fx-min-width: 200px; " +
  15. "-fx-min-height: 450px; " +
  16. "-fx-max-width: 200px; " +
  17. "-fx-max-height: 450px;");
  18. return list;
  19. }
  20. public Button createBtn() {
  21. Button btn = new Button();
  22. btn.setText("Create a Project");
  23. btn.setTextFill(Color.WHITE);
  24. btn.setFont(Font.font("Times New Roman", FontWeight.BOLD, 15));
  25. btn.setStyle ("-fx-background-color: #283B4E;" + "-fx-border-radius: 500;" + "-fx-background-radius: 8em; " +
  26. "-fx-min-width: 150px; " +
  27. "-fx-min-height: 60px; " +
  28. "-fx-max-width: 150px; " +
  29. "-fx-max-height: 60px;" +
  30. "-fx-effect: dropShadow(gaussian, rgba(0, 0, 0, 0.3), 15, 0.5, 0.5, 0);");
  31. return btn;
  32. }
  33. public Button dashboardBtn() {
  34. Button btn = new Button();
  35. btn.setMinWidth(150);
  36. btn.setMinHeight(70);
  37. btn.setText("Dashboard");
  38. btn.setTextFill(Color.WHITE);
  39. btn.setFont(Font.font("Times New Roman", FontWeight.BOLD, 15));
  40. btn.setStyle ("-fx-background-color: #283B4E;" + "-fx-background-radius: 8em; " +
  41. "-fx-min-width: 150px; " +
  42. "-fx-min-height: 60px; " +
  43. "-fx-max-width: 150px; " +
  44. "-fx-max-height: 60px;" +
  45. "-fx-effect: dropShadow(gaussian, rgba(0, 0, 0, 0.3), 15, 0.5, 0.5, 0);");
  46. return btn;
  47. }
  48. }

答案1

得分: 0

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

  1. import javafx.application.Application;
  2. import javafx.scene.*;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.layout.BorderPane;
  5. import javafx.scene.layout.HBox;
  6. import javafx.stage.Stage;
  7. public class App extends Application {
  8. public static void main(String[] args) {
  9. launch(App.class);
  10. }
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. interfaceLayout ui = new interfaceLayout();
  14. BorderPane root = new BorderPane();
  15. Button createBtnOne = ui.createBtn();
  16. createBtnOne.setOnAction((t) -> {
  17. System.out.println("You pressed creatBtnOne");
  18. });
  19. Button dashboardBtnOne = ui.dashboardBtn();
  20. dashboardBtnOne.setOnAction((t) -> {
  21. System.out.println("You pressed dashboardBtnOne");
  22. });
  23. HBox topHb = new HBox(createBtnOne, dashboardBtnOne);
  24. topHb.setSpacing(10);
  25. root.setTop(topHb);
  26. topHb.setStyle("-fx-background-color: #34475A;" +
  27. "-fx-set-spacing: 10;" +
  28. "-fx-padding: 10;");
  29. HBox leftHb = new HBox(ui.currentProjects());
  30. leftHb.setSpacing(10);
  31. root.setLeft(leftHb);
  32. leftHb.setStyle("-fx-background-color: #34475A; -fx-set-spacing: 10; -fx-padding: 10;");
  33. primaryStage.setTitle("Project Management");
  34. primaryStage.setScene(new Scene(root, 1000, 600));
  35. primaryStage.show();
  36. }
  37. }
英文:

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

  1. import javafx.application.Application;
  2. import javafx.scene.*;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.layout.BorderPane;
  5. import javafx.scene.layout.HBox;
  6. import javafx.stage.Stage;
  7. public class App extends Application {
  8. public static void main(String[] args) {
  9. launch(App.class);
  10. }
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. interfaceLayout ui = new interfaceLayout();
  14. BorderPane root = new BorderPane();
  15. Button createBtnOne = ui.createBtn();
  16. createBtnOne.setOnAction((t) -> {
  17. System.out.println("You pressed creatBtnOne");
  18. });
  19. Button dashboardBtnOne = ui.dashboardBtn();
  20. dashboardBtnOne.setOnAction((t) -> {
  21. System.out.println("You pressed dashboardBtnOne");
  22. });
  23. HBox topHb = new HBox(createBtnOne, dashboardBtnOne);
  24. topHb.setSpacing(10);
  25. root.setTop(topHb);
  26. topHb.setStyle("-fx-background-color: #34475A;" +
  27. "-fx-set-spacing: 10;" +
  28. "-fx-padding: 10;");
  29. HBox leftHb = new HBox(ui.currentProjects());
  30. leftHb.setSpacing(10);
  31. root.setLeft(leftHb);
  32. leftHb.setStyle("-fx-background-color: #34475A; -fx-set-spacing: 10; -fx-padding: 10;");
  33. primaryStage.setTitle("Project Management");
  34. primaryStage.setScene(new Scene(root, 1000, 600));
  35. primaryStage.show();
  36. }
  37. }

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:

确定