我第一次使用 Java 和 JavaFX,我尝试运行这个程序,但遇到了各种各样的错误。 该应用程序由 YourController.java 组成,并附加到 modMenue.fxml 文件,现在它只是给出此错误
Error: Main method not found in class application.YourController, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
关于如何解决的任何想法?
这是所有代码:
package application;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.stage.DirectoryChooser;
public class YourController implements Initializable {
@FXML
private ChoiceBox<String> versionChoiceBox;
@FXML
private Button versionNext;
@FXML
private Button modFile;
@FXML
private Label label;
@FXML
private Stage stage;
public void setVersions(ObservableList<String> versions) {
versionChoiceBox.setItems(versions);
versionChoiceBox.getSelectionModel().selectedIndexProperty();
versionChoiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, selectedVersion) -> {
System.out.println("Selected Version : " + selectedVersion);
//Button versionNext = new Button("versionButton");
// Bind the disable property of the button to the selection status of the ChoiceBox
versionNext.disableProperty().bind(versionChoiceBox.getSelectionModel().selectedItemProperty().isNull());
});
}
// when initialize1 is merged into initialize it breaks everything
public void initialize1(URL url, ResourceBundle resourceBundle) {
versionNext.setDisable(true); // Disable the button initially
versionChoiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, selectedVersion) -> {
System.out.println("Selected Version: " + selectedVersion);
versionNext.setDisable(selectedVersion == null);
});
}
//scene switcher to the mod folder selection
public void switchTomodMenue() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("modMenue.fxml"));
Scene scene = new Scene(root);
stage = (Stage) versionNext.getScene().getWindow();
stage.setScene(scene);
stage.show();
}
// for opening file directory
public void openDirectoryChooser() {
DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(stage);
if (selectedDirectory != null) {
System.out.println("Selected directory: " + selectedDirectory.getAbsolutePath());
} else {
System.out.println("No directory selected.");
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
我尝试添加一个“public static void main(String[] args)”,但这只是不返回任何内容
问题常常出现在访问“浏览”按钮时,我想拉出文件管理器来选择文件,但它会说 getScene() 返回 null