当我在 Eclipse 中运行代码时,Java 窗口会打开,但 JavaFX 中没有 GUI。我检查了 Eclipse 中的控制台,没有任何错误。
我什至在不同的目录中重写了该项目,但我得到了相同的结果。
我该如何解决这个问题?
Main.java
package application;
import javafx.application.Application;
public class Main extends Application {
public void start(Stage primaryStage) {
try {
FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
BorderPane root1 = new BorderPane();
Scene scene = new Scene(root1,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
public class MainController implements Initializable {
@FXML private TableView<Product> table;
@FXML private TableColumn<Product, String> Code;
@FXML private TableColumn<Product, String> Brand;
@FXML private TableColumn<Product, String> Type;
@FXML private TableColumn<Product, String> Colour;
@FXML private TableColumn<Product, String> Size;
@FXML private TableColumn<Product, Integer> Quantity;
public ObservableList<Product> list = FXCollections.observableArrayList(
new Product ("GD05", "Gildan", "T-Shirt", "Black", "Medium", 10),
new Product ("GD05", "Gildan", "T-Shirt", "Black", "Large", 5),
new Product ("GD05", "Gildan", "T-Shirt", "Black", "X Large", 3),
new Product ("GD05", "Gildan", "T-Shirt", "Black", "XX Large", 1)
);
@Override
public void initialize(URL location, ResourceBundle resources) {
Code.setCellValueFactory(new PropertyValueFactory<Product, String>("Code"));
Brand.setCellValueFactory(new PropertyValueFactory<Product, String>("Brand"));
Type.setCellValueFactory(new PropertyValueFactory<Product, String>("Type"));
Colour.setCellValueFactory(new PropertyValueFactory<Product, String>("Colour"));
Size.setCellValueFactory(new PropertyValueFactory<Product, String>("Size"));
Quantity.setCellValueFactory(new PropertyValueFactory<Product, Integer>("Quantity"));
table.setItems(list);
}
}
最佳答案
它正在加载 GUI:它只是不显示它,因为您没有要求它这样做。
public void start(Stage primaryStage) {
try {
// loads the FXML, but discards the result:
FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
// creates a new, empty BorderPane
BorderPane root1 = new BorderPane();
// makes the empty border pane the root of the scene:
Scene scene = new Scene(root1,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
// sets the scene in the stage and shows it:
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
你需要这样的东西:
public void start(Stage primaryStage) {
try {
BorderPane root1 = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root1,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
这假设您的 FXML 文件的根元素是 BorderPane
,我猜这就是这里的意图。如果没有,您可以将 BorderPane
更改为相关类型,或者您可以这样做
BorderPane root1 = new BorderPane();
root1.setCenter(FXMLLoader.load(...));
关于JavaFX 未加载 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938707/