我需要为我的一门Java课程做一个游戏(否则我对Java的面向对象编程有一些基本了解):我在上创建了一个javaFX场景(使用JFXFrame在摆动框架上进行)一类是我的视图,然后还有另外两类:处理游戏的和用于创建Player 对象的另一类。
现在,当游戏运行时,玩家轮流玩,我想显示currentPlayer的名称:我将该属性添加到Game类中,并添加了Player的名称是SimpleStringProperty,因为我阅读它后可以更轻松地使用侦听器。
问题是:我不知道该怎么做...对不起,我的问题似乎很愚蠢,但是我确实已经尝试了两个星期来 使用监听器 ,它没有任何作用:我的javaFX场景没有更新(我们看不到currentPlayer名称的更改)。 我阅读了有关 binding 的信息,认为它可能会有所帮助,但我也不知道如何使用。
有什么想法或解释吗?非常感谢 !
这是我的代码:
//视图
public class TestView {
protected static Game game = new Game("Random Game");
protected static BorderPane root = new BorderPane();
protected static GridPane grilleHaut = new GridPane();
public static void main(String[] args) throws InterruptedException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestView.initialisation();
}
});
game.runSomething();
}
public static void initialisation() {
JFrame mainSwingFrame = new JFrame("Game");
JFXPanel mainFXFrame = new JFXPanel();
mainSwingFrame.add(mainFXFrame);
mainSwingFrame.setSize(1200, 900);
mainSwingFrame.setVisible(true);
mainSwingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Platform.runLater(new Runnable() {
public void run() { initFX(mainFXFrame);}
} );
}
public static void initFX(JFXPanel frameFX) {
Scene scene = newScene();
frameFX.setScene(scene);
}
public static Scene newScene() {
Scene scene = new Scene(root, Color.LIGHTGOLDENRODYELLOW);
Text gameName = new Text(game.name);
grilleHaut.add(gameName, 0, 0);
GridPane.setConstraints(gameName, 0, 0, 1, 1, HPos.CENTER, VPos.TOP);
Text turn = new Text("It's " + game.currentPlayer.getName() + "'s turn to play.");
game.currentPlayer.name.addListener((observable, oldValue, newValue) -> {
turn.setText("C'est au tour de : " + newValue);
});
game.currentPlayer.name.addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> o,String oldv,String newv) {
turn.setText("It's time to play for : " + newv);
System.out.println("accessed on TestView.");
}
});
grilleHaut.add(turn, 0, 1);
GridPane.setConstraints(turn, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER);
BorderPane.setAlignment(grilleHaut, Pos.TOP_RIGHT);
root.setTop(grilleHaut);
return(scene);
}
}
//一个游戏
class Game {
String name;
Player p1 = new Player("Person 1");
Player p2 = new Player("Human 2");
Player currentPlayer;
public Game(String n) {
name=n;
currentPlayer = p1;
}
public void runSomething() {
//here goes code to run a game
System.out.println(currentPlayer.getName());
currentPlayer = p2; //at some point in the game, it's p2's time to play
String pressToContinue; Scanner ptc = new Scanner(System.in); pressToContinue = ptc.nextLine();
System.out.println(currentPlayer.getName());
currentPlayer = p1; //and then p1's again
String pressToContinue2; Scanner ptc2 = new Scanner(System.in); pressToContinue2 = ptc.nextLine();
System.out.println(currentPlayer.getName());
}
}
//玩家
class Player {
protected StringProperty name = new SimpleStringProperty();
public Player(String n) {
name.setValue(n);
name.addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> o,String oldv,String newv) {
Text turn = new Text("It's time to play for : " + newv);
TestView.grilleHaut.add(turn, 0, 1);
System.out.println("accessed on Player.");
}
});}
public String getName() { return name.getValue(); }
}