Weird error in JavaFX

Question:
there is the weird error appering

Repl link:
Ephraim mart (JavaFX) - Replit

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.geometry.Pos;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import static java.lang.System.out;
// lets you type "out.println();"instead of "System.out.println();"
import java.util.Scanner;
// lets access the Scanner class
public class Main extends Application { 
  static String txt;
  @Override
  public void start(Stage primaryStage) {
    
    Label label; 
    TextField tf;
    Button yButton;
    Button nButton;
    VBox vbox;
    HBox hbox;
    Scene scene;
    
    // tf = new TextField("Yes/No");
    // tf.setMaxWidth(200);
    txt = """
          Hello! Welcome to \"Ephraim's SuperStore\"!
          In this store you can: dine at the
          legendary EPHRAIM'S FAST-FOOD BAR,
          buy quality produce at Ephraim mart,
          use our quality ATM, or play on the
          slots machine

          First, I have a question for you. Are you
          the owner or CEO of this magnificent store?
                      """;
    label = new Label(txt);
    yButton = new Button("YES"); 
    nButton = new Button("NO");
    yButton.setTranslateX(-50);
    yButton.setTranslateY(20);
    nButton.setTranslateX(50);
    nButton.setTranslateY(10);

    vbox = new VBox(label);
    vbox.setSpacing(5);
    vbox.setAlignment(Pos.CENTER);
    hbox = new HBox(yButton, nButton);
    scene = new Scene(vbox, hbox, 600, 400);
    
    primaryStage.setTitle("EphraimMart");
    primaryStage.setScene(scene);
    primaryStage.show();
  } 
    
  public static void main(String[] args) {
    launch(args);
    Scanner input = new Scanner(System.in);
      out.print("\033[H\033[2J");
      out.flush();
      // an ASCI code to clear the console
    out.println("Which version do you want: \n 1) JavaFX (graphical) \n 2) Java (text)?");
    String answer = input.nextLine();
    if (answer.equals("1")) {
      out.println("Please watch the output screen and click fullscreen");
      launch(args);
    } else {
      EPStore.initialize();
    }
  }
} 

note: this this happened after I added this

 txt = """
          Hello! Welcome to \"Ephraim's SuperStore\"!
          In this store you can: dine at the
          legendary EPHRAIM'S FAST-FOOD BAR,
          buy quality produce at Ephraim mart,
          use our quality ATM, or play on the
          slots machine

          First, I have a question for you. Are you
          the owner or CEO of this magnificent store?
                      """;

Just a thing, nothing to do with your problem. You are calling launch(args); twice. You can’t call launch method more than once in a JavaFX

public static void main(String[] args) {
    launch(args); // Calling here
    Scanner input = new Scanner(System.in);
      out.print("\033[H\033[2J");
      out.flush();
      // an ASCI code to clear the console
    out.println("Which version do you want: \n 1) JavaFX (graphical) \n 2) Java (text)?");
    String answer = input.nextLine();
    if (answer.equals("1")) {
      out.println("Please watch the output screen and click fullscreen");
      launch(args); //And calling here
    } else {
      EPStore.initialize();
    }

Also this is wrong:

According to docs: Scene (JavaFX 20)

The Scene class doesn’t have a constructor that takes two nodes (vbox and hbox that you are using) and two double values (like 600 and 400). So you should construct the scene taking these for account.

Something like this:

VBox vbox = new VBox();
HBox hbox = new HBox(yButton, nButton);
vbox.getChildren().addAll(label, hbox);
vbox.setSpacing(5);
vbox.setAlignment(Pos.CENTER);

Scene scene = new Scene(vbox, 600, 400);

primaryStage.setTitle("EphraimMart");
primaryStage.setScene(scene);
primaryStage.show();

About the weird letters…
I don’t remember if Replit support ASCII escape sequences. Try to comment them and run the code again. If it works it’s because Replit does not support it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.