本帖最后由 wow7jiao 于 2023-7-7 18:42 编辑
就差一点点,表里不出数据,我不知道怎么设置控制器MainController ,应该是scene里面设置或者样表改,请前辈解决package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,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);
}
}
package application;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.beans.property.*;
import java.net.URL;
import java.util.ResourceBundle;
public class MainController implements Initializable {
@FXML
private TableView<Person> table;
@FXML
private TableColumn<Person, Integer> ageCol;
@FXML
private TableColumn<Person, String> nameCol;
// 创建并初始化数据
private final ObservableList<Person> cellData = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
cellData.add(new Person(20,"Jack"));
cellData.add(new Person(18,"Jerry"));
cellData.add(new Person(21,"Mike"));
// 配置 TableColumn 列
ageCol.setCellValueFactory(cellData -> cellData.getValue().getAge().asObject());
nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());
// 为 TableView 添加数据
table.setItems(cellData);
}
public class Person {
private IntegerProperty age;
private StringProperty name;
public Person(Integer age, String name) {
super();
this.age = new SimpleIntegerProperty(age);
this.name = new SimpleStringProperty(name);
}
public IntegerProperty getAge() {
return age;
}
public void setAge(Integer age) {
this.age = new SimpleIntegerProperty(age);
}
public StringProperty getName() {
return name;
}
public void setName(String name) {
this.name = new SimpleStringProperty(name);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<center>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="ageCol" prefWidth="75.0" text="C1" />
<TableColumn fx:id="nameCol" prefWidth="75.0" text="C2" />
</columns>
</TableView>
</center>
</BorderPane>
根据报错信息,错误的原因是找不到"MainController"类。请确保在`Sample.fxml`文件中正确指定了`controller`属性,并且`MainController`类位于`application`包中。
具体来说,你需要在`Sample.fxml`文件中添加一个`fx:controller`属性,指向`MainController`类的完全限定名,例如:
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="application.MainController">
...
</BorderPane>
这样,FXML加载器才能正确地将`Sample.fxml`与`MainController`关联起来。
|