Home > Software engineering >  Create a Button that Opens a new Tab in JavaFX
Create a Button that Opens a new Tab in JavaFX

Time:11-10

I am creating a game. For that game, there should be a "Play" Button (already added) and a rules button (the one I am trying to add). For some reason, when I try to add the rules button, it never shows up in the screen. Inside the rules tab I just want to add the rule content of the game. Any suggestions? Here is my code:

package com.example.jaxafx_practice;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.IOException;
//import com.example.jaxafx_practice.Playscene.playscence;


public class Main extends Application {
    Scene StartScene;
    @Override
    public void start(Stage primary) throws IOException {

        // ********Start scene************
        Button PlayButton = new Button("Play");
        PlayButton.setMinWidth(55);
        PlayButton.setMinHeight(30);
        
        VBox layout1 = new VBox(250.0);
        layout1.setAlignment(Pos.CENTER);
        Label StartSceneLabel = new Label("Wordle");
         StartSceneLabel.setMinWidth(55);
        StartSceneLabel.setMinHeight(30);
        StartSceneLabel.setAlignment(Pos.CENTER);

        StartSceneLabel.setStyle("-fx-background-color: #FFFFFF;");
        layout1.getChildren().addAll(StartSceneLabel, PlayButton);
        primary.setTitle("Start screen");
        layout1.setStyle("-fx-background-color:#800000");
        HBox user_interface=new HBox();


        // Creates Start scene
        StartScene = new Scene(layout1, 500, 750.0);

        // ********Start scene************



        //????????Play scene?????????????
        Playscene playscene = new Playscene();
        //????????Play scene?????????????

        //Create the word to be guessed
        Choose_word Choose_word = new Choose_word();
        String wordToGuess = Choose_word.getWord();


        //Button actions
        PlayButton.setOnAction((e) -> {
            primary.setScene(playscene.playscence());
            primary.setTitle("Game Screen");});
        primary.setScene(StartScene);
        primary.show();
        playscene.enterButton.setOnAction((e) -> {
            String userWord=playscene.guessField.getText();
            if(userWord.length()==4)
                {playscene.displayWord(userWord.toLowerCase(),wordToGuess);
                playscene.count =1;}
        });
        primary.setScene(StartScene);
        primary.show();
    }
    public static void main() {
        launch();
    }
}




CodePudding user response:

If this line

layout1.getChildren().addAll(StartSceneLabel, PlayButton);

is where you add the rules button, my guess it's because your layout1 gap is huge. The spacing your suggesting is 250. So adding another component in this list might exceed your stage size. For a test, change

VBox layout1 = new VBox(250.0);

to

VBox layout1 = new VBox(25.0);

And then add your button again. I'm guessing this is the issue because like @VGR said, the button isn't there, nor do you have a tabpane yet.

CodePudding user response:

Give this question a look, I'm not sure if this is what you want but give it a shot. How to create a “add tab“button in JAVAFX?

  • Related