我正在尝试为我一直维护的项目设计一个高分菜单,我希望能够使用.txt或.csv文件来存储和检索此项目的所有高分,以显示在我用scenebuilder构建的高分菜单上,但我在这样做时遇到了麻烦。到目前为止,我已经尝试使用传统的方法读取文件,但我似乎无法将其输入到scenebuilder的文本区域。感谢您的帮助。谢谢。
高分.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.HighscoreController">
<children>
<VBox prefHeight="400.0" prefWidth="601.0">
<children>
<StackPane prefHeight="77.0" prefWidth="601.0" style="-fx-background-color: #242322;">
<children>
<Label text="Highscores" textFill="#045a61">
<font>
<Font name="Wingdings" size="62.0" />
</font>
</Label>
</children>
</StackPane>
<StackPane layoutX="10.0" layoutY="10.0" prefHeight="327.0" prefWidth="601.0">
<children>
<TableView fx:id="table">
<columns>
<TableColumn fx:id="numColumn" prefWidth="322.6665954589844" text="No." />
<TableColumn fx:id="nameColumn" prefWidth="201.33331298828125" text="Name" />
<TableColumn fx:id="scoreColumn" prefWidth="81.3333740234375" text="Score" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</StackPane>
</children>
</VBox>
</children>
</AnchorPane>
高分.java
package Model;
import java.io.File;
import java.util.Scanner;
public class Highscore {
int numColumn, ScoreColumn;
String nameColumn;
public int getNumColumn() {
return numColumn;
}
public void setNumColumn(int numColumn) {
this.numColumn = numColumn;
}
public int getScoreColumn() {
return ScoreColumn;
}
public void setScoreColumn(int scoreColumn) {
ScoreColumn = scoreColumn;
}
public String getNameColumn() {
return nameColumn;
}
public void setNameColumn(String nameColumn) {
this.nameColumn = nameColumn;
}
public Highscore(int numColumn, int ScoreColumn, String nameColumn){
this.numColumn = numColumn;
this.ScoreColumn = ScoreColumn;
this.nameColumn = nameColumn;
}
}
highscorecontroller.java:
package Controller;
import Model.Highscore;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
public class HighscoreController implements Initializable {
@FXML
private TableView<Highscore> table;
@FXML
private TableColumn<Highscore, Integer> numColumn;
@FXML
private TableColumn<Highscore, String> nameColumn;
@FXML
private TableColumn<Highscore, Integer> scoreColumn;
String fileName = "src/main/resources/Misc/Highscore.csv";
File file = new File(fileName);
{
try {
Scanner inputStream = new Scanner(file);
while ((inputStream.hasNext())){
String data = inputStream.next();
Object[] values = data.split("\n");
Object[] values_line = data.split(",");
// Name[0] = (String)values[0];
// Score[0] = (Integer)values[1];
// Num[0] = (Integer)values[2];
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
ObservableList<Highscore> list = FXCollections.observableArrayList(
new Highscore(1, 200, "Bob")
);
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
numColumn.setCellValueFactory(new PropertyValueFactory<Highscore, Integer>("numColumn"));
nameColumn.setCellValueFactory(new PropertyValueFactory<Highscore, String>("nameColumn"));
scoreColumn.setCellValueFactory(new PropertyValueFactory<Highscore, Integer>("scoreColumn"));
table.setItems(list);
}
}
2条答案
按热度按时间pgvzfuti1#
你得把它放在箱子里
list
,列表是您的项目源,并且ObservableList
因此列表中的更改将触发视图中的更改,绑定在:table.setItems(list);
```ObservableList list = FXCollections.observableArrayList(
new Highscore(1, 200, "Bob")
);
// Name[0] = (String)values[0];
// Score[0] = (Integer)values[1];
// Num[0] = (Integer)values[2];
list.add(new Highscore(Integer.valueOf(values_line[0].toString()),Integer.valueOf(values_line[1].toString()),values_line[2].toString()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//list of Highscore items
ObservableList list = FXCollections.observableArrayList();
// Name[0] = (String)values[0];
// Score[0] = (Integer)values[1];
// Num[0] = (Integer)values[2];
list.add(new Highscore(Integer.valueOf(values_line[0]),Integer.valueOf(values_line[1]),values_line[2]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
l0oc07j22#
您可以使用streamapi加载文件并创建
HighScore
物体。然后你可以创建一个ObservableList
其中之一HighScore
物体。