第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在國際象棋游戲的網(wǎng)格窗格中交換節(jié)點

在國際象棋游戲的網(wǎng)格窗格中交換節(jié)點

DIEA 2023-04-26 15:09:27
我無法單擊一個按鈕,然后單擊另一個按鈕與之前單擊的按鈕交換。然后將第一個單擊的按鈕設為空白按鈕。我想不出該怎么做。我只是嘗試使用 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();    }}
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經(jīng)驗 獲得超3個贊

以下是交換 a 中的兩個按鈕的mreGridPane

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);

? ? }

}


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號