JavaFX没有加载GUI

时间:2018-02-22 23:07:41

标签: 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);

}

}

2 个答案:

答案 0 :(得分:1)

它正在加载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(...));

答案 1 :(得分:0)

必须输入以下值  一个容器。

FXMLLoader.load(getClass().getResource("/application/Main.fxml"));

BorderPane root1;
root1 = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));