JavaFX 未加载 GUI

标签 java eclipse javafx tableview

当我在 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/

相关文章:

c++ - Windows 中用于 C、C++ 的 Eclipse Juno 问题

JavaFX KeyEvent 未触发

java - 将 ActiveMQ 服务与 Web 服务集成 (Mule ESB)

java - 如何在 eclipse java 应用程序中添加 java 参数以及类路径?

java - 在 Android 上使用 Teechart 制作动画

java - 如何在 Moqui 框架中配置实体外观以禁用检查实体存在

JavaFX:在迭代时删除特殊日期

javafx - IBM jdk 上的 Java FX 2?

java - 使用 Java 在 Spark 2.0 中使用数据集的 GroupByKey

java - [学习EJB] : annotation type is not applicable to this kind of declaration?