这个问题可能是同一个问题的这一个,但现在看来,一个的提问者还没有添加足够的信息来获得一个有用的响应。
我正在尝试使用JDK和JavaFx SDK版本11.0.2运行JavaFx应用程序。
此代码完全按预期工作,从而产生一个空窗口:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 420, 420));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试向添加标签StackPane,则会引发异常。
import ...
import javafx.scene.control.Label;
public class Main extends Application {
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
root.getChildren().add(new Label("42"));
primaryStage.setScene(new Scene(root, 420, 420));
primaryStage.show();
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 fxml 文件填充示例 javafx TableView。
这是我的控制器方法:
public class TestController implements Initializable {
@FXML private TableView<user> tableView;
@FXML private TableColumn<user, String> UserId;
@FXML private TableColumn<user, String> UserName;
public void initialize(URL location, ResourceBundle resources) {
UserId.setCellValueFactory(new PropertyValueFactory<user, String>("userId"));
UserName.setCellValueFactory(new PropertyValueFactory<user, String>("userName"));
tableView.getItems().setAll(parseUserList());
}
private List<user> parseUserList(){
List<user> l_u = new ArrayList<user>();
user u = new user();
u.setUserId(1);
u.setUserName("test1");
l_u.add(u);
u.setUserId(2);
u.setUserName("test2");
l_u.add(u);
u.setUserId(3);
u.setUserName("test3");
l_u.add(u);
return l_u;
}
}
Run Code Online (Sandbox Code Playgroud)
和 fxml 文件:
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="369.0" prefWidth="505.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="viewmodel.TestController …Run Code Online (Sandbox Code Playgroud)