简体   繁体   English

JavaFX 构造函数和改变场景/阶段

[英]JavaFX constructors and changing scene/stage

everyone reading每个人都在阅读

Currently trying to figure out the basics of JavaFX can somebody help me with how to set a new scene using a constructor from another class.目前试图弄清楚 JavaFX 的基础知识,有人可以帮助我如何使用来自另一个 class 的构造函数来设置新场景。 I tried the obvious things to attempt but to no avail except for errors being thrown my way.我尝试了明显的尝试,但无济于事,除了错误被抛出。 I guess I just do not understand how to change the stage from my main stage to the classes' stage when choosing a specific menu item.我想我只是不明白在选择特定菜单项时如何将舞台从主舞台更改为课程舞台。 I need to figure this out before attempting any of the actual coding for the classes.在尝试对类进行任何实际编码之前,我需要弄清楚这一点。

Thanks for any help!谢谢你的帮助!

Main主要的

    package application;

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.MenuItem;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;

    public class Main extends Application implements EventHandler<ActionEvent>
    {
    Pane pane;
    MenuItem item1, item2, item3, item4;
    Label label;
    Scene scene;
    Menu menu;
    
    
   public static void main(String args[]){
      launch(args);
   }
   
    @Override
       public void start(Stage primaryStage) 
       {
           menu = new Menu("Choose");
           
           item1 = new MenuItem("Add Food");
           item2 = new MenuItem("Add Exercise");
           item3 = new MenuItem("View Log");
           item4 = new MenuItem("Exit");
           
           menu.getItems().addAll(item1, item2, item3, item4);
           
           MenuBar menuBar = new MenuBar(menu);
           menuBar.setTranslateX(200);
           menuBar.setTranslateY(100);
           
           item1.setOnAction(this);
           item2.setOnAction(this);
           item3.setOnAction(this);
           item4.setOnAction(this);
              
           pane = new StackPane();
           
           label = new Label("Welcome to the Health Tracker!\nWhat would you like to do?");
           
           pane.getChildren().addAll(label,menuBar);
           scene = new Scene(pane, 400, 400);
           
           primaryStage.setTitle("Health Tracker");
           
           primaryStage.setScene(scene);
           primaryStage.show();
       }
 

    @Override
    public void handle(ActionEvent event) 
    {
        if(event.getSource() == item1)
        {
            food getfood = new food();
        }
        else if(event.getSource() == item2)
        {
            exercise getExercise = new exercise();
        }
        else if(event.getSource() == item3)
        {
            System.out.print("log");
        }
        else if(event.getSource() == item4)
        {
            System.out.print("exited");
        }
            
    }

    }

Class I am trying to change to Class 我正在尝试更改为

package application;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class food implements EventHandler<ActionEvent>
{
Pane pane;
MenuItem item1, item2;
Label label;
Scene scene;
Menu menu;
Stage primaryStage;

public void start(Stage primaryStage)
{
    menu = new Menu("Choose");
       
    item1 = new MenuItem("Add Food");
    item2 = new MenuItem("Exit");
    
    menu.getItems().addAll(item1, item2);
    
    MenuBar menuBar = new MenuBar(menu);
    menuBar.setTranslateX(200);
    menuBar.setTranslateY(100);
    
    item1.setOnAction(this);
    item2.setOnAction(this);
    
    pane = new StackPane();
    
    label = new Label("Welcome to the Food tracker!\nWhat would you like to do?");
    
    pane.getChildren().addAll(label,menuBar);
    scene = new Scene(pane, 400, 400);
    
    primaryStage.setTitle("Food Tracker");
       
    primaryStage.setScene(scene);
    primaryStage.show();
}

@Override
public void handle(ActionEvent event) 
{
    if(event.getSource() == item1)
    {
        
    }
    if(event.getSource() == item2)
    {
        
    }
    
}

public food()
{
    primaryStage.show();
}
}

Thanks again for any help:)再次感谢任何帮助:)

There are many things you can correct in the code:您可以在代码中纠正许多事情:

  • Naming conventions.命名约定。
  • No need to implement EventHandler for every class.无需为每个 class 实现 EventHandler。
  • Creating Stage instance in Food class.在 Food class 中创建 Stage 实例。 And no need for naming start() method in all classes.并且不需要在所有类中命名 start() 方法。

To answer your question, you need to modify the constructor of the Food class as below.要回答您的问题,您需要修改 Food class 的构造函数,如下所示。

public Food(){
    primaryStage = new Stage();
    start(primaryStage);
}

Having said that, you can optimize the code a lot if you understand the JavaFX concepts correctly.话虽如此,如果您正确理解 JavaFX 概念,则可以对代码进行很多优化。 I strongly suggest to go through any JavaFX tutorial.我强烈建议 go 通过任何 JavaFX 教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM