0

My java program doesn't compile and I am simply looking to get to the point where my java program runs the FXML file and simply displays the GUI. I don't know how to get to that point.

I used Scenebuilder to writes the code for the GUI but I am not sure how attach the design code to the code so that it displays.

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.*;

public class Main extends Application {

private JTextField guess_space; // text field for user's guess
private JLabel bottom_Lab; // label for too high/too low output
private int theNumber; // The number we are trying to find

public void checkGuess() { // Method that checks Hi-Lo of guess
    String guessText = guess_space.getText();
    String message;

    // Check the guess for too high/too low
    int guess = Integer.parseInt(guessText);

    // if the guess is too high
    if (guess > theNumber) {
        message = guess + " was too high. Guess again!";
        bottom_Lab.setText(message);
    }
    // if the guess is too low
    else if (guess < theNumber) {
        message = guess + " was too low. Guess again!";
        bottom_Lab.setText(message);
    }
    else {
        message = guess + " was the correct guess. You Win!!!";
    }
}


public void newGame() {
    theNumber = (int)(Math.random()* 100 + 1);
}


@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("GuessingGame.fxml"));
    primaryStage.setTitle("Guessing Game");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();

    Main theGame = new Main();
    theGame.newGame();
}


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

}

Controller.java

package sample;

import xxx

public class Controller {

}
Marco R.
  • 2,667
  • 15
  • 33
Bestdes
  • 51
  • 3
  • 2
    In addition to not including a stack trace, you say "compile error" but describe a runtime error. – chrylis -cautiouslyoptimistic- May 21 '19 at 03:06
  • 1
    (1) Please [edit] your question to include the full [stack trace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). (2) Why are you creating a new `Main` instance inside the `start` method? You already have an instance—the one `start` was invoked on. (3) Swing is not the same thing as JavaFX. The classes in the `javax.swing` package (e.g. `JTextField` and `JLabel`) are used in Swing. – Slaw May 21 '19 at 03:30

1 Answers1

1

Swing and JavaFX are two different java based frameworks tailored to building desktop UIs; and they do not naturally interoperate. Even when you are mostly using JavaFX, you are including JTextField and JLabel swing components in your application.

Abra
  • 19,142
  • 7
  • 29
  • 41
Marco R.
  • 2,667
  • 15
  • 33