Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在网格窗格中交换节点以进行国际象棋游戏_Java_Javafx_Gridpane - Fatal编程技术网

Java 在网格窗格中交换节点以进行国际象棋游戏

Java 在网格窗格中交换节点以进行国际象棋游戏,java,javafx,gridpane,Java,Javafx,Gridpane,我无法单击一个按钮,然后单击另一个按钮以与以前单击的按钮交换。然后将第一个单击的按钮设置为空白按钮 我想不出怎么做。我只是试着用if语句 If button = this{ //this should find the first button if button = that{ //this should find the second button that = this //this

我无法单击一个按钮,然后单击另一个按钮以与以前单击的按钮交换。然后将第一个单击的按钮设置为空白按钮

我想不出怎么做。我只是试着用if语句

If button = this{                 //this should find the first button
    if button = that{             //this should find the second button
       that = this                //this swaps the buttons
    }
   this = blank                  //and ends with making the first button 
blank
}
这不起作用,因为它直接传递了第二个if语句,并使第一个按钮为空,而不交换任何内容

没有太多的代码,这只是测试代码,以找出这个单一的行动

public class SimpleButtonSwapper extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        GridPane pane = new GridPane();
        pane.setPadding(new Insets(50, 50, 50, 50));           //creates 50 pixel padding
        pane.setVgap(2);
        pane.setHgap(2);

        Scene scene = new Scene(pane);

        Canvas canvas = new Canvas(50, 400);
        GraphicsContext graphics = canvas.getGraphicsContext2D();
        pane.getChildren().add(canvas);

        stage.setTitle("Chess");

        Button[] buttons = new Button[6];

        for (int i = 0; i < 6; i++) {
            buttons[i] = new Button();
            buttons[i].setText("banana " + i);
        }

        for (int i = 0; i < 6; i++) {
            pane.add(buttons[i], i, 0);
        }

        for (int i = 0; i < 6; i++) {

            buttons[i].setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {

                }
            });
        }

        stage.setScene(scene);

        stage.show();
    }
}
公共类SimpleButtonSwapper扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
GridPane=新建GridPane();
setPadding(新插入(50,50,50,50));//创建50像素的填充
窗格。设置间隙(2);
窗格。setHgap(2);
场景=新场景(窗格);
画布=新画布(50400);
GraphicsContext graphics=canvas.getGraphicsContext2D();
pane.getChildren().add(画布);
舞台剧名(“国际象棋”);
按钮[]按钮=新按钮[6];
对于(int i=0;i<6;i++){
按钮[i]=新按钮();
按钮[i].setText(“香蕉”+i);
}
对于(int i=0;i<6;i++){
添加(按钮[i],i,0);
}
对于(int i=0;i<6;i++){
按钮[i].setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
}
});
}
舞台场景;
stage.show();
}
}

您可以创建一个属性来保存所选按钮状态。。。。但我认为“改变文本”并不能真正帮助你创造一个国际象棋游戏。我会有一个“棋子”对象及其基本规则/属性

 SimpleObjectProperty<Button> selectedButtonProperty = new SimpleObjectProperty<>();

 for (int i = 0; i < 6; i++)
 {

     buttons[i].setOnAction(new EventHandler<ActionEvent>()
     {
         @Override
         public void handle(ActionEvent event)
         {
             Button thisButton = (Button) event.getSource();
             if (selectedButtonProperty.get() == null)
             {
                selectedButtonProperty.set(thisButton);
             } else
             {
                 thisButton.setText(selectedButtonProperty.get().getText());
                 selectedButtonProperty.get().setText(null);
                 selectedButtonProperty.set(null);
             }

         }
     });

  }
SimpleObjectProperty selectedButtonProperty=new SimpleObjectProperty();
对于(int i=0;i<6;i++)
{
按钮[i].setOnAction(新的EventHandler()
{
@凌驾
公共无效句柄(ActionEvent事件)
{
Button thisButton=(Button)event.getSource();
if(selectedButtonProperty.get()==null)
{
SelectedButton属性设置(此按钮);
}否则
{
thisButton.setText(selectedButtonProperty.get().getText());
selectedButtonProperty.get().setText(null);
selectedButtonProperty.set(空);
}
}
});
}

您可以创建一个属性来保存所选按钮状态。。。。但我认为“改变文本”并不能真正帮助你创造一个国际象棋游戏。我会有一个“棋子”对象及其基本规则/属性

 SimpleObjectProperty<Button> selectedButtonProperty = new SimpleObjectProperty<>();

 for (int i = 0; i < 6; i++)
 {

     buttons[i].setOnAction(new EventHandler<ActionEvent>()
     {
         @Override
         public void handle(ActionEvent event)
         {
             Button thisButton = (Button) event.getSource();
             if (selectedButtonProperty.get() == null)
             {
                selectedButtonProperty.set(thisButton);
             } else
             {
                 thisButton.setText(selectedButtonProperty.get().getText());
                 selectedButtonProperty.get().setText(null);
                 selectedButtonProperty.set(null);
             }

         }
     });

  }
SimpleObjectProperty selectedButtonProperty=new SimpleObjectProperty();
对于(int i=0;i<6;i++)
{
按钮[i].setOnAction(新的EventHandler()
{
@凌驾
公共无效句柄(ActionEvent事件)
{
Button thisButton=(Button)event.getSource();
if(selectedButtonProperty.get()==null)
{
SelectedButton属性设置(此按钮);
}否则
{
thisButton.setText(selectedButtonProperty.get().getText());
selectedButtonProperty.get().setText(null);
selectedButtonProperty.set(空);
}
}
});
}

我真的不知道这在国际象棋游戏中有什么用处,但下面是我在网格窗格中交换两个按钮时所做的

我为第一次单击的按钮创建了一个变量,该变量在单击按钮之前将保持“null”,因此第一次单击将在“first”中保存单击的按钮,第二次单击将交换并删除“first”的内容

要交换它们,我必须检索它们在gridPane中的位置(我只保存了列索引,您可能还需要保存行索引)


我真的不知道这在国际象棋游戏中如何有用,但下面是我在网格窗格中交换两个按钮时所做的

我为第一次单击的按钮创建了一个变量,该变量在单击按钮之前将保持“null”,因此第一次单击将在“first”中保存单击的按钮,第二次单击将交换并删除“first”的内容

要交换它们,我必须检索它们在gridPane中的位置(我只保存了列索引,您可能还需要保存行索引)

以下是在
网格窗格中交换两个按钮的示例:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class FxMain extends Application {

    private static final int COLS = 5, ROWS = 5;
    private int clickCounter = 0;
    private GridPane grid;
    private Button first, second;

    @Override
    public void start(Stage primaryStage){

        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        root.getChildren().addAll(makeGrid(), new Text("Click 2 buttons to swap them"));
        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    private Pane makeGrid() {

        grid = new GridPane();
        for(int rowIndex = 0; rowIndex < ROWS ; rowIndex++) {
            //an array to hold buttons of one row 
            Node[] nodes = new Node[COLS];
            for(int colIndex = 0; colIndex < COLS ; colIndex++) {
                Button node= new Button(rowIndex+""+colIndex);
                node.setOnAction(e->buttonCliked(e.getSource())); //add action listener 
                node.prefHeight(100); node.prefWidth(100);
                nodes[colIndex]= node;
            }
            grid.addRow(rowIndex, nodes);
        }
        return grid;
    }

    private void buttonCliked(Object source) {

        if(!(source instanceof Button)) return;
        Button button  = (Button)source;

        if(clickCounter == 0){
            first = button;
        }else{
            second = button;
            swap();
        }

        System.out.println(clickCounter + " " + ((Button)source).getText()    );
        clickCounter=  ++clickCounter %2 ;  // changes values between 0 1
    }

    private void swap() {
        int firstRow = GridPane.getRowIndex(first);
        int firstCol = GridPane.getColumnIndex(first);
        int secondRow = GridPane.getRowIndex(second);
        int secondCol = GridPane.getColumnIndex(second);
        grid.getChildren().removeAll(first, second);
        grid.add(first, secondCol, secondRow);
        grid.add(second, firstCol, firstRow);
    }

    public static void main(final String[] args) {
        launch(args);
    }
}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.Pane;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类FxMain扩展了应用程序{
私有静态final int COLS=5,行=5;
私有int clickCounter=0;
私有网格;
私人按钮第一,第二;
@凌驾
公共无效开始(阶段primaryStage){
VBox根=新的VBox(10);
根。设置填充(新插图(10));
root.getChildren().addAll(makeGrid(),新文本(“单击两个按钮以交换它们”);
primaryStage.setScene(新场景(根));
primaryStage.sizeToScene();
primaryStage.show();
}
私有窗格makeGrid(){
grid=新的GridPane();
对于(int-rowIndex=0;rowIndexbuttonCliked(e.getSource());//添加操作侦听器
节点预处理高度(100);节点预处理宽度(100);
节点[colIndex]=节点;