我无法单击一个按钮,然后单击另一个按钮以与之前单击的按钮交换。然后将第一个单击的按钮变成空白按钮。
我想不出该怎么做。我只是尝试使用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();
}
}
答案 0 :(得分:0)
您可以创建一个属性来保存选定的按钮状态。...但是我认为“更改文本”并不能真正帮助您创建国际象棋游戏。我将拥有一个“ ChessPiece”对象及其基本规则/属性...
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);
}
}
});
}
答案 1 :(得分:0)
我真的不知道这在国际象棋游戏中如何有用,但这是我在gridPane中交换两个按钮的操作
我为第一次单击的按钮创建了一个变量,该变量在您单击一个按钮之前将一直为“ null”,因此第一次单击会将被单击的按钮保存在“ first”中,第二次单击将交换并擦除其中的内容。 “第一”
要交换它们,我必须检索它们在gridPane中的位置(我只保存了列索引,并且可能还需要保存行索引)
//looping through the buttons
for (Button button : buttons) {
//adding a listener
button.setOnAction(e -> {
if (first == null)
first = button;
else {
if (first != button) {
Button second = button;
int index1 = GridPane.getColumnIndex(first);
int index2 = GridPane.getColumnIndex(second);
pane.getChildren().removeAll(first, second);
pane.add(first, index2, 0);
pane.add(second, index1, 0);
}
first = null;
}
});
}
答案 2 :(得分:0)
以下是mre,在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++) {
Node[] nodes = new Node[COLS];
for(int colIndex = 0; colIndex < COLS ; colIndex++) {
Button node= new Button(rowIndex+""+colIndex);
node.setOnAction(e->buttonCliked(e.getSource()));
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);
}
}