如何在javafx中刷新窗格
我最近一直在学习JavaFX,对如何刷新窗格有一个问题。在这个简单的程序中,当我单击“移动”按钮时,我希望黑色方块向右移动到下一个块中,但它不起作用,我想知道如何修复代码 主要类别:如何在javafx中刷新窗格,javafx,Javafx,我最近一直在学习JavaFX,对如何刷新窗格有一个问题。在这个简单的程序中,当我单击“移动”按钮时,我希望黑色方块向右移动到下一个块中,但它不起作用,我想知道如何修复代码 主要类别: public class Main extends Application { private Cell cells[] = new Cell[5]; private Player player; private Board board; Button move = new But
public class Main extends Application {
private Cell cells[] = new Cell[5];
private Player player;
private Board board;
Button move = new Button("move");
public Main() throws Exception {
for (int i = 0; i < cells.length; i++) {
cells[i] = new Cell(i);
}
this.player = new Player(0, cells);
this.board = new Board(player, cells);
}
@Override
public void start(Stage primaryStage) throws Exception {
Main game = new Main();
BorderPane pane = new BorderPane();
pane.setCenter(board);
pane.setBottom(move);
Scene scene = new Scene(pane,400,80);
primaryStage.setTitle("Move");
primaryStage.setScene(scene);
primaryStage.show();
move.setOnAction(e -> game.move());
}
public void move() {
player.currentCell.index += 1;
board.paint();
}
public static void main(String[] args) {
launch(args);
}
}
单元类别:
class Cell {
public int index;
public Cell(int index) {
this.index = index;
}
}
你可能想改写你的代码,将玩家的位置存储在
player
类中会让你的生活变得困难。我还建议在单元格
类中添加一个标志,说明玩家是否在里面,例如
class Cell {
public int index;
private Player playerInCell;
public Cell(int index) {
this.index = index;
}
public void setPlayerInCell(Player p){
this.playerInCell = p;
}
public void clearPlayerInCell(){
this.playerInCell = null;
}
public Player getPlayerInCell(){
return this.playerInCell;
}
}
然后,将播放机移动到单元格
时,您可以将其从以前的单元格
中清除,并将其设置到新单元格和Paint()
函数中,如果播放机存在,则将单元格上色
另一件事,如果您希望坚持使用您的方法,您的问题是因为您只更改了单元格
类上的索引
属性,您还应该更改单元格
在数组中的位置单元格[]
或仅更改Player
类的currentCell
属性,否则您的播放器将始终保持在同一位置。下面是更改Player
的currentCell
属性的示例:
public void move() {
Cell currentCell = player.currentCell;
Cell nextCell = null;
for (int i = 0; i < cells.length; i++) {
if (cells[i] == currentCell && i+1 < cells.length){
nextCell = cells[i+1];
break;
}
}
if (nextCell != null)
player.currentCell = nextCell;
else{
//Error handling, next cell not found
}
board.paint();
}
玩家
public class Player {
private Cell currentCell;
public Player(Cell cell) throws Exception {
this.currentCell = cell;
cell.setPlayerInCell(this);
}
public Cell getCurrentCell(){
return this.currentCell;
}
public void setCurrentCell(Cell cell){
this.currentCell = cell;
}
}
单元格
public class Cell {
private int index;
private Player playerInCell;
public Cell(int index) {
this.index = index;
}
public void setPlayerInCell(Player p){
this.playerInCell = p;
}
public void clearPlayerInCell(){
this.playerInCell = null;
}
public Player getPlayerInCell(){
return this.playerInCell;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
现在可以了,我可以移动单元格了,我也设置了它,这样当玩家到达终点时,单元格可以回到起点,但这就是一个例子。
它使用playerInCell
的Cell
属性工作,如果该属性不为空,则我们知道有玩家在单元格中,可以将其涂成黑色。如果为空,则单元格中没有玩家,我们可以将其涂成白色。这也允许你在未来拥有更多不同颜色的球员。虽然我不知道你的最终目标是什么。希望这对你有所帮助,如果你想要任何进一步的解释,请随时询问
另外,为了进一步阅读,看看为什么像我这样使用getter和setter更好
此外,这段代码背后的原因是:
move.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
move();
}
});
move.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
move();
}
});
因为我使用的是Java1.7而不是Java1.8,并且不能使用谓词,所以您可以安全地将其更改为
move.setOnAction(e->this.move())改为code>。您检查了我发布的代码了吗?这解决了您的问题了吗?为什么要创建Main
的第二个实例?可以是move.setOnAction(e->this.move())代码>同时,您正在将越来越多的矩形
添加到板
,但永远不要删除它们。这是故意的吗?更新了答案并在我的上运行,希望helpsIt仍然不起作用,在我点击按钮后,player的currentCell发生了变化,但黑色方块没有移动。啊,我看到了另一个问题,一会儿
public class Board extends Pane {
private Player player;
private Cell cells[];
private final int CELLWIDTH = 40;
private final int CELLHEIGHT = 40;
private final int LMARGIN = 100;
public Board(Player p, Cell cells[]) {
player = p;
this.cells = cells;
paint();
}
public Cell[] getCells(){
return this.cells;
}
public Player getPlayer() {
return player;
}
public void paint() {
//Clear previous cells, we don't need them now
getChildren().clear();
//Redraw them
for(Cell cell : cells){
Rectangle r1 = new Rectangle(xCor(cell.getIndex()), 0, CELLWIDTH, CELLHEIGHT);
r1.setStroke(Color.BLACK);
//We've found a player in the cell, let's colour it black
if (cell.getPlayerInCell() != null)
r1.setFill(Color.BLACK);
//No, player in this cell, white it is
else
r1.setFill(Color.WHITE);
getChildren().add(r1);
}
}
private int xCor(int col) {
return LMARGIN + col * CELLWIDTH;
}
}
public class Player {
private Cell currentCell;
public Player(Cell cell) throws Exception {
this.currentCell = cell;
cell.setPlayerInCell(this);
}
public Cell getCurrentCell(){
return this.currentCell;
}
public void setCurrentCell(Cell cell){
this.currentCell = cell;
}
}
public class Cell {
private int index;
private Player playerInCell;
public Cell(int index) {
this.index = index;
}
public void setPlayerInCell(Player p){
this.playerInCell = p;
}
public void clearPlayerInCell(){
this.playerInCell = null;
}
public Player getPlayerInCell(){
return this.playerInCell;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
move.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
move();
}
});