我的Java程序无法编译,我只是想让我的Java程序运行FXML文件并仅显示GUI。我不知道该怎么做。

我使用Scenebuilder编写了GUI的代码,但不确定如何将设计代码附加到代码上以使其显示。

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 {

}

最佳答案

SwingJavaFX是为构建桌面UI量身定制的两个不同的基于Java的框架;并且它们自然不会互操作。即使当您主要使用JavaFX时,您仍在应用程序中包含JTextFieldJLabel swing组件。

08-06 04:20