我正在为我制作的这个小型棋盘游戏应用程序使用javafx构建UI。它只有Othello和Connect Four,您也可以添加球员以保持得分。分数仅记录在.txt文件中,并在程序启动时加载。
主菜单是带有记分板的场景,该记分板是从.txt文件创建的,还包括按钮。我可以添加播放器,然后将它们写入文件并加载就可以了,当我尝试在.txt文件中重新加载播放器并刷新记分板时,就会出现问题。我尝试刷新场景,但记分牌被打印两次。
代码如下:
import java.io.*;
import java.util.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
public class Main extends Application {
static Stage window;
public void refreshMainMenu() {
Button c4button = new Button("Connect Four");
Button othelloButton = new Button("Othello");
Button addPlayerButton = new Button("Add New Player");
Button quitButton = new Button("Quit");
GridPane scoreboard = new GridPane();
if(!Player.loadPlayers()) {
Label label = new Label("No Players Yet");
scoreboard.add(label, 0, 0);
}
else {
Label label = new Label("Scoreboard:\n");
Label temp1 = new Label("Name\t");
Label temp2 = new Label("Othello Wins\t");
Label temp3 = new Label("Connect Four Wins\t");
Label temp4 = new Label("Total Wins\t");
scoreboard.add(label, 0, 0);
scoreboard.add(temp1, 0, 1);
scoreboard.add(temp2, 1, 1);
scoreboard.add(temp3, 2, 1);
scoreboard.add(temp4, 3, 1);
for(int i = 0; i < Player.getPlayerList().size(); i++) {
Label temp5 = new Label(Player.getPlayerList().get(i).getName());
Label temp6 = new Label(Integer.toString(Player.getPlayerList().get(i).getOthelloWins()));
Label temp7 = new Label(Integer.toString(Player.getPlayerList().get(i).getConnectFourWins()));
Label temp8 = new Label(Integer.toString(Player.getPlayerList().get(i).getTotalWins()));
scoreboard.add(temp5, 0, i+2);
scoreboard.add(temp6, 1, i+2);
scoreboard.add(temp7, 2, i+2);
scoreboard.add(temp8, 3, i+2);
}
}
scoreboard.setAlignment(Pos.CENTER);
HBox buttons = new HBox(20);
buttons.getChildren().addAll(c4button, othelloButton, addPlayerButton, quitButton);
buttons.setAlignment(Pos.CENTER);
VBox layout = new VBox(10);
layout.getChildren().addAll(scoreboard, buttons);
layout.setAlignment(Pos.CENTER);
Scene mainMenu = new Scene(layout, 600, 300);
window.setScene(mainMenu);
window.show();
c4button.setOnAction(e -> {
try {
ConnectFour.playConnectFour();
} catch (IOException ex) {
ex.printStackTrace();
}
});
othelloButton.setOnAction(e -> {
try {
Othello.playOthello();
} catch (IOException ex) {
ex.printStackTrace();
}
});
addPlayerButton.setOnAction(e -> {
try {
String newPlayer = TextBox.display("Add Player", "Enter Player Name:");
Player.addNewPlayer(newPlayer);
} catch (IOException ex) {
ex.printStackTrace();
}
refreshMainMenu();
});
quitButton.setOnAction(e -> {
boolean response = ConfirmBox.display("Exit Board Master","Are you sure?");
if(response)
window.close();
});
window.setOnCloseRequest(e -> {
e.consume();
boolean response = ConfirmBox.display("Exit Board Master","Are you sure?");
if(response)
window.close();
});
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Board Master");
refreshMainMenu();
}
public static void main(String[] args) throws IOException {
launch(args);
}
}
还有确认和文本框弹出窗口:
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
public class ConfirmBox {
private static boolean response;
static boolean display(String title, String message) {
Stage window = new Stage();
window.setTitle(title);
window.initModality(Modality.APPLICATION_MODAL);
Button yesButton = new Button("Yes");
Button noButton = new Button("No");
yesButton.setOnAction(e -> {
response = true;
window.close();
});
noButton.setOnAction(e -> {
response = false;
window.close();
});
Label label = new Label(message);
StackPane toplayout = new StackPane(label);
HBox centerlayout = new HBox(10);
centerlayout.getChildren().addAll(yesButton, noButton);
centerlayout.setAlignment(Pos.CENTER);
BorderPane layout = new BorderPane();
layout.setPadding(new Insets(5,5,5,5));
layout.setTop(toplayout);
layout.setCenter(centerlayout);
Scene alertBox = new Scene(layout, 250, 60);
window.setScene(alertBox);
window.showAndWait();
return response;
}
}
并且:
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
public class TextBox {
private static String response;
static String display(String title, String message) {
Stage window = new Stage();
window.setTitle(title);
window.initModality(Modality.APPLICATION_MODAL);
Label label = new Label(message);
TextField input = new TextField();
Button confirmButton = new Button("Confirm");
confirmButton.setOnAction(e -> {
response = input.getText();
window.close();
});
VBox layout = new VBox(5);
layout.getChildren().addAll(label, input, confirmButton);
Scene textBox = new Scene(layout, 250, 80);
window.setScene(textBox);
window.showAndWait();
return response;
}
}
和播放器类:
import java.io.*;
import java.util.*;
public class Player {
private String name;
private int totalWins = 0;
private int othelloWins = 0;
private int connectFourWins = 0;
private static ArrayList<Player> playerList = new ArrayList<>(1);
private Player(String input) {
name = input;
}
private Player(String input, int othello, int connectFour, int total) {
name = input;
othelloWins = othello;
connectFourWins = connectFour;
totalWins = total;
}
void addOthelloWin() {
othelloWins++;
totalWins++;
}
void addConnectFourWin() {
connectFourWins++;
totalWins++;
}
void changeName(String input) {
name = input;
}
static ArrayList<Player> getPlayerList() {
return playerList;
}
static Player getPlayer(String input) {
for(int i = 0; i < playerList.size(); i++) {
if(playerList.get(i).name.equals(input)) {
return playerList.get(i);
}
}
return null;
}
String getName() {
return name;
}
int getOthelloWins() {
return othelloWins;
}
int getConnectFourWins() {
return connectFourWins;
}
int getTotalWins() {
return totalWins;
}
static void printWins() {
System.out.println("Names\t\tOthello\t\tConnect4\t\tTotal" + "\n");
for(int i = 0; i < playerList.size(); i++)
System.out.println(playerList.get(i).name + "\t\t\t" + playerList.get(i).othelloWins + "\t\t\t" +
playerList.get(i).connectFourWins + "\t\t\t" + playerList.get(i).totalWins);
System.out.println();
}
static void addNewPlayer(String name) throws IOException {
Player newPlayer = new Player(name);
playerList.add(newPlayer);
File file = new File("player_Data.txt");
FileWriter writer = new FileWriter(file, true);
writer.write(newPlayer.name + "\t\t" + newPlayer.othelloWins + "\t\t" + newPlayer.connectFourWins + "\t\t" + newPlayer.totalWins + System.lineSeparator());
writer.close();
}
static boolean loadPlayers(){
Scanner input;
try {
File file = new File("player_Data.txt");
input = new Scanner(file);
} catch (FileNotFoundException e) {
return false;
}
while (input.hasNextLine()) {
try {
String line = input.nextLine();
String[] lineArray = line.split("\t\t");
Player newPlayer = new Player(lineArray[0], Integer.parseInt(lineArray[1]), Integer.parseInt(lineArray[2]), Integer.parseInt(lineArray[3]));
playerList.add(newPlayer);
} catch (Exception e) {
System.out.println("Player data incorrectly formatted.");
break;
}
}
return true;
}
static void recordWins() throws IOException {
File file = new File("player_Data.txt");
FileWriter writer = new FileWriter(file);
for(int i = 0; i < playerList.size(); i++) {
writer.write(playerList.get(i).name + "\t\t" + playerList.get(i).othelloWins + "\t\t" + playerList.get(i).connectFourWins + "\t\t" + playerList.get(i).totalWins + System.lineSeparator());
}
writer.close();
}
}
这是一个示例运行: Opening with no data
Scoreboard showing twice after adding a player.
这是我第一次尝试javafx应用程序,所以也许我对它的工作方式感到困惑。我的想法是我们只有一个窗口可以显示不同的场景。我有一个场景是主菜单,它由refreshMainMenu()显示。每当我们添加新的播放器时,主菜单都会刷新,但是如果单击“ Othello”或“连接四”按钮,则将切换到Othello的场景或“连接四”的场景。这就是为什么在用户单击添加新播放器并添加播放器之后,我调用refreshMainMenu(),它将使用新播放器数据从头开始再次创建主菜单场景的原因。我尝试将移动到调用refreshMainMenu()的位置,还尝试关闭窗口,然后调用refreshMainMenu()。关闭窗口并再次调用刷新会导致出现相同的记分板问题。
这也是我在这里的第一篇文章,对不起,如果我违反任何规则或之前曾提出问题,我们深感抱歉。我进行了搜索,但找不到任何对我的案子有真正帮助的帖子。也有一些类可以运行Othello和Connect Four游戏,但是由于我还没有到达那里,它们现在仍在控制台中运行。如有必要,我可以添加代码。
答案 0 :(得分:0)
调用refreshMainMenu()
方法会使玩家列表上的现有玩家加倍。 if(!Player.loadPlayers())
加载txt文件中存储的播放器,而无需检查播放器是否已经存储在Player.playerList
列表中。
我决定提供帮助,因为您付出了很多努力来创建问题,并且根据代码的质量,我可以看到您刚刚开始使用JavaFx。几个好的建议。您不需要每次刷新时都更改场景。这是非常不好的做法。了解JavaFx中的可观察集合。玩家不应存储玩家列表。首先-学习使用调试器。