Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

java - How to create multiple scenes in one stage?

So I have this GUI code for a patient and employee management, I want to add a form to each menu but can't because I need to add another scene in the same stage. I keep getting a duplicate error(tried to use the same name) when I tried to use the same scene. How do I go about adding multiple scenes in JavaFX?

import javafx.geometry.VPos;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class FinalsP extends Application{
    
    @Override
    public void start(Stage stage) throws Exception {
        Button btn= new Button();
        btn.setText("Log");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent Event) {
                System.out.println("Hello");
            }
        });
        Label eName = new Label("Employee Name:");
        TextField empTf = new TextField();
        // root.addColumn( 0,eName,empTf );

        GridPane root = new GridPane(); 
        root.setHgap(10);
        root.setVgap(2);
        root.addColumn( 0,eName,empTf );
        root.setPadding(new Insets(0, 10, 0, 200));
        Scene scn=new Scene(root);

        //menu 1
        MenuBar menuBar = new MenuBar();
        VBox vBox = new VBox(menuBar);
        Scene scene = new Scene(vBox, 960, 600);
        Menu menu1 = new Menu("Menu 1");
        menuBar.getMenus().add(menu1);

        //menu 2
        MenuBar mBar = new MenuBar();
        VBox vBoxx = new VBox(mBar);                
        Menu menu2 = new Menu("Menu 2");
        menuBar.getMenus().add(menu2);

        //menu 3
        MenuBar mBar1 = new MenuBar();
        VBox vBoxx1 = new VBox(mBar1);
        Menu menu3 = new Menu("Menu 3");
        menuBar.getMenus().add(menu3);

        //menu 4
        MenuBar mBar2 = new MenuBar();
        VBox vBoxx2 = new VBox(mBar2);
        Menu menu4 = new Menu("Menu 4");
        menuBar.getMenus().add(menu4);

        //menu 5
        MenuBar mBar3 = new MenuBar();
        VBox vBoxx3 = new VBox(mBar3);
        Menu menu5 = new Menu("Menu 5");
        menuBar.getMenus().add(menu5);

        //menu 6
        MenuBar mBar4 = new MenuBar();
        VBox vBoxx4 = new VBox(mBar4);
        Menu menu6 = new Menu("Menu 6");
        menuBar.getMenus().add(menu6);

        stage.setScene(scene);
        stage.setTitle("M.O.M");
        stage.show();
    }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...