gpt4 book ai didi

java - ListView 项目不会出现在 JavaFx 应用程序中

转载 作者:行者123 更新时间:2023-11-29 04:31:53 24 4
gpt4 key购买 nike

出于某种原因,列表中的文本未填充指定的 ListView。字符串列表应显示在左上角的网格 Pane 中。我能做什么?

主类:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
// Select main layout file
Parent root = FXMLLoader.load(getClass().getResource("/resources/static/fxml/scene.fxml"));

// Set Scene window params
primaryStage.setTitle("React");
primaryStage.setScene(new Scene(root, 1150, 600));
primaryStage.setResizable(false);

// Set task bar primary icon
primaryStage.getIcons().add(new javafx.scene.image.Image("/resources/static/images/react-app-icon.png"));

// Show Scene
primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}

ListView Controller :

public class ListViewController implements Initializable {

@FXML private ListView<String> listView1;

@Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<String> data = FXCollections.observableArrayList(
"Ticket 1","Ticket 2","Ticket 3","Ticket 4"
);
listView1.setItems(data);
}
}

场景:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>

<Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
<children>
<GridPane layoutX="63.0" layoutY="72.0" prefHeight="500.0" prefWidth="1072.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ScrollPane prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.columnIndex="1" GridPane.rowIndex="1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
<children>
<ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #FFE0B2;" /> <!-- Orange -->
</children>
</AnchorPane>
</content>
</ScrollPane>
<ScrollPane prefHeight="250.0" prefWidth="520.0" vbarPolicy="ALWAYS">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
<children>
<ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #C8E6C9;" fx:id="listView1" /> <!-- Green -->
</children>
</AnchorPane>
</content>
</ScrollPane>
<ScrollPane prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.columnIndex="1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
<children>
<ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #FFCCBC;" /> <!-- Red -->
</children>
</AnchorPane>
</content>
</ScrollPane>
<ScrollPane prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.rowIndex="1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
<children>
<ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #FFF9C4;" /> <!-- Yellow -->
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</GridPane>
<ToolBar maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="35.0" prefWidth="1200.0" style="-fx-background-color: #FF5001;">
<items>
<ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/react-toolbar-logo.png" />
</image>
</ImageView>
</items>
</ToolBar>
</children>
</Pane>

最佳答案

您没有在 FXML 文件中指定 Controller 类。你需要

<Pane fx:controller="my.package.ListViewController" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">

<!-- ... -->

</Pane>

(将 my.package 替换为 Controller 类的实际包名)。

关于java - ListView 项目不会出现在 JavaFx 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43476673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com