我无法单击一个按钮,然后单击另一个按钮与之前单击的按钮交换。然后将第一个单击的按钮设为空白按钮。
我想不出该怎么做。我只是尝试使用 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();
}
}
一只名叫tom的猫
相关分类