Java fxml app not working - Cannot find symbol error

旧时模样 提交于 2019-12-20 06:40:19

问题


i downloaded a java fxml app from https://github.com/HassanAlthaf/AlarmApplication and when i try to run it a get a "cannot find symbol" error from the MainView.java class. Here is the code from the Mainview.java file

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
public class MainView implements Initializable {

@FXML
private TextField hoursField;

@FXML
private TextField minutesField;

@FXML
private TextField secondsField;

private final AlarmController alarmController;

@FXML
private void startAlarm(ActionEvent event) {
    int response = this.alarmController.startAlarm(
        this.hoursField.getText(), 
        this.minutesField.getText(),
        this.secondsField.getText()
    );

    if (response == Alarm.ERROR_INVALID_INPUT) {
        this.showWarning("Please enter a number only for the times!", "Invalid input received");
    } else if (response == Alarm.ERROR_ALARM_ALREADY_ON) {
        this.showWarning("An alarm is already running at the moment! If you wish to override the current alarm, please stop it first!", "Cannot override current alarm.");
    } else if (response == Alarm.ERROR_NO_TIME_SET) {
        this.showWarning("You cannot set an alarm for 0 seconds! Please set a realistic time.", "No time set");
    } else {
        this.showSuccessfulMessage("Successfully scheduled alarm!", "Success");
    }
}

public void showWarning(String message, String title) {
    Alert alert = new Alert(AlertType.WARNING);
    alert.setTitle(title);
    alert.setHeaderText(null);
    alert.setContentText(message);
    alert.showAndWait();
}

public void showSuccessfulMessage(String message, String title) {
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.setTitle(title);
    alert.setHeaderText(null);
    alert.setContentText(message);
    alert.showAndWait();
}

@FXML
private void stopAlarm(ActionEvent event) {
    this.alarmController.stopAlarm();

    this.showSuccessfulMessage("Successfully stopped the alarm!", "Success");
}


public MainView() {
    this.alarmController = new AlarmController();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

}    

}


回答1:


Your error message should be similar to the following:

[javac] cannot find symbol

[javac] import javafx.scene.control.Alert;

[javac] ..........................................^

[javac] symbol: class Alert

As it has been said in the comments: The Alert class has been introduced in Java 8u40. For further changes that have been introduced on the 8u40 release see this Oracle blog post.

So: Update your Java distribution, either by downloading it on the Oracle page, or by updating it via sudo apt-get update && sudo apt-get install openjdk-8-jdk openjfx -y



来源:https://stackoverflow.com/questions/43982820/java-fxml-app-not-working-cannot-find-symbol-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!