我正在试图解决这个问题并搜索了很多内容。但我找不到任何东西。
这是我第一次在StackOverflow上提问。
如果我无法正确解释我的问题,请耐心等待。
任何帮助都会受到赞赏。
这是我的代码:
package Main_Game;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import Models.character;
import Models.gameObject;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class leaderBoard
{
private static int id = 0;
leaderBoard()
{
//Stage created
Stage primaryStage = new Stage();
//root pane
BorderPane root = new BorderPane();
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
//grid pane for top header
GridPane gridHeader = new GridPane();
gridHeader.setHgap(140);
root.setMargin(gridHeader, new Insets(30,0,0,0));
//scene created with borderpane as root
Scene scene = new Scene(root,1200,700);
//setting background of whole scene
final String BACKGROUND = "-fx-background-color: #2A2927;";
//setting style of header
final String HEADER_STYLE = "-fx-font-family: 'Bungee Shade';"
+ "-fx-font-size: 40;";
final String TEXT_STYLE = "-fx-font-size: 25;"
+ "-fx-font-family: 'Calibri';";
root.setStyle(BACKGROUND);
//Header for rank text
Text rankHeader = new Text();
rankHeader.setText("Rank#");
rankHeader.setFill(Color.YELLOW);
rankHeader.setStyle(HEADER_STYLE);
//Header for name text
Text nameHeader = new Text();
nameHeader.setText("Name");
nameHeader.setFill(Color.YELLOW);
nameHeader.setStyle(HEADER_STYLE);
//header for score text
Text scoreHeader = new Text();
scoreHeader.setText("Score");
scoreHeader.setFill(Color.YELLOW);
scoreHeader.setStyle(HEADER_STYLE);
//
Text rank = new Text();
rank.setStyle(TEXT_STYLE);
rank.setFill(Color.WHITE);
Text name = new Text();
name.setStyle(TEXT_STYLE);
name.setFill(Color.WHITE);
Text score = new Text();
score.setStyle(TEXT_STYLE);
score.setFill(Color.WHITE);
//totalScore variable gets total score from another java file
String totalScore = Integer.toString(gameObject.getTotalScore());
//playerName variable gets character's name from another java file
String playerName = character.getName();
//Using BufferedWriter to write playerName and totalScore in a text file
BufferedWriter bw = null;
try
{
File file = new File("score.txt");
//if file does not exit it will create a new file
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file,true);
bw = new BufferedWriter(fw);
/*Want to increment ID everytime file gets written but that's not main concern for now
*
* file gets written with playerName and totalScore
*
* I have used \t to create space between ID, name and score
*/
bw.write(id++ + "\t\t\t\t\t\t\t\t" + playerName + "\t\t\t\t\t\t\t" + totalScore);
//After every line....another line starts with new line
bw.newLine();
bw.flush();
bw.close();
}
catch(IOException e)
{
e.printStackTrace();
}
/*
* Using BufferedReader to read from file and outputting it in my scene
*/
BufferedReader br = null;
try
{
FileReader fr = new FileReader("score.txt");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null)
{
//Splitting words so that I can print out ID, Name and Score
//If i don't use split it just prints out score...because score gets written at last in file
String[] words = line.split(",");
for(int i =0; i<words.length; i++)
{
name.setText(words[i]);
/*
* HERE IS THE PROBLEM
* Every time it reads line from text file
* I add it to gridPane below Header
* but i want to add rows dynamically but i get illegalArgumentException
*/
gridPane.add(name,2,1);
}
}
br.close();
}
catch(IOException e)
{
e.printStackTrace();
}
/*
* IGNORE THIS PART
* I tried using Scanner but it won't work
*/
// Scanner sc = null;
// try
// {
// File file = new File("score.txt");
// sc = new Scanner(file);
// }
// catch(IOException e)
// {
// e.printStackTrace();
// }
//
// while(sc.hasNextLine())
// {
// Scanner sc2 = new Scanner(sc.nextLine());
// while(sc2.hasNext())
// {
// String s = sc2.next();
// score.setText(s);
// }
// }
gridHeader.add(rankHeader, 0, 0);
gridHeader.add(nameHeader, 1, 0);
gridHeader.add(scoreHeader, 2, 0);
root.setTop(gridHeader);
root.setCenter(gridPane);
primaryStage.setScene(scene);
primaryStage.setTitle("Basket Bud");
primaryStage.show();
}
}
答案 0 :(得分:1)
您正在使用多次添加相同的对象。即你正在向窗格中添加相同的“名称”变量,在Javafx中是一个很大的没有,更好的方法是
ArrayList<Text> myText = new ArrayList<Text>();
try
{
FileReader fr = new FileReader("score.txt");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null)
{
//Splitting words so that I can print out ID, Name and Score
//If i don't use split it just prints out score...because score gets written at last in file
String[] words = line.split(",");
for(int i =0; i<words.length; i++)
{
Text temp = new Text();
temp.setText(words[i]);
temp.setStyle(TEXT_STYLE);
temp.setFill(Color.WHITE);
myText.add(temp) //allows you to edit these later
/*
* HERE IS THE PROBLEM
* Every time it reads line from text file
* I add it to gridPane below Header
* but i want to add rows dynamically but i get illegalArgumentException
*/
gridPane.add(temp,2,i+1);
}
}
br.close();
}
对不起,手机上无法进行无法识别
如果这不起作用告诉我