遇到编译器错误:应用程序启动方法中的异常。 (这是简单的JavaFX程序)

时间:2019-05-21 02:08:56

标签: java user-interface intellij-idea javafx scenebuilder

我的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 {

}

1 个答案:

答案 0 :(得分:0)

SwingJavaFX以及两个用于构建桌面UI的基于Java的不同框架;并且它们自然不会互操作。即使您主要使用JavaFX,也将在应用程序中包含JTextFieldJLabel摇摆组件。