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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Javafx 如何在 GridPane 中定位特定按鈕

Javafx 如何在 GridPane 中定位特定按鈕

一只斗牛犬 2023-06-08 14:43:37
我正在制作帶有按鈕的西洋跳棋游戲。要?dú)⑺懒硪粔K,你必須將你的塊沿對(duì)角線移動(dòng)到另一塊上,但我不確定如何確保你的塊移動(dòng)到另一塊上。我解決這個(gè)問(wèn)題的想法是獲取第二個(gè)按鈕的行和列,這是你的作品移動(dòng)到的按鈕,然后從每個(gè)行和列中減去 1,然后從該按鈕獲取文本以測(cè)試它是否是“黑色”或“紅色”。第一和第二 = 按鈕System.out.println((GridPane.getColumnIndex(second) + " vs " + (GridPane.getColumnIndex(second) - 1)));    if (GridPane.getColumnIndex(second) > 0) {        System.out.println("checking if a button has been jumped");        GridPane.setRowIndex(second, (GridPane.getRowIndex(second) - 1));        GridPane.setColumnIndex(second, (GridPane.getColumnIndex(second) - 1));        System.out.println("this is a printing of the second button name for location " + (GridPane.getColumnIndex(second)) + " " + (GridPane.getRowIndex(second)) + " " + second.getText());        if (second.getText().contains("black")) {            System.out.println("it's a kill");        }         else {            System.out.println("no kill");            GridPane.setRowIndex(second, (GridPane.getRowIndex(second) + 1));            GridPane.setColumnIndex(second, (GridPane.getColumnIndex(second) + 1));        }    }我可以將行和列更改為與另一部分的位置匹配的內(nèi)容,但是當(dāng)我從該按鈕(第二個(gè))獲取文本時(shí),它不會(huì)以名稱“黑色”或“紅色”返回,而只是空白按鈕的名稱。我的猜測(cè)是 GridPane 可能不會(huì)像這樣工作,我只需要想出另一個(gè)解決方案,希望我不必將整個(gè)代碼重做為二維數(shù)組或其他東西。
查看完整描述

2 回答

?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

GridPane您可以通過(guò)遍歷子節(jié)點(diǎn)來(lái)獲取對(duì)節(jié)點(diǎn)的引用GridPane。
添加一些簡(jiǎn)單的計(jì)算來(lái)找到被點(diǎn)擊的兩個(gè)按鈕之間的對(duì)角線,如果有的話:

import java.awt.Toolkit;

import javafx.animation.PauseTransition;

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;

import javafx.util.Duration;


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 find the \n diagonally between 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(node)); //add action listener

? ? ? ? ? ? ? ? nodes[colIndex]= node;

? ? ? ? ? ? }

? ? ? ? ? ? grid.addRow(rowIndex, nodes);

? ? ? ? }

? ? ? ? return grid;

? ? }


? ? private void buttonCliked(Button button) {


? ? ? ? if(clickCounter == 0){

? ? ? ? ? ? first = button;

? ? ? ? }else{

? ? ? ? ? ? second = button;

? ? ? ? ? ? markNode(findMidDiagonalButton());

? ? ? ? }


? ? ? ? System.out.println(clickCounter + " " + button.getText()? ? );

? ? ? ? clickCounter=? ++clickCounter %2 ;? // changes values between 0 1

? ? }


? ? //change node background for a short while, and then reset it

? ? private void markNode(Node node) {


? ? ? ? if(node == null) return;

? ? ? ? String style = node.getStyle();

? ? ? ? node.setStyle("-fx-background-color: cornflowerblue;");

? ? ? ? PauseTransition pause = new PauseTransition(Duration.seconds(1));

? ? ? ? pause.play();

? ? ? ? pause.setOnFinished(e-> node.setStyle(style));

? ? }


? ? private Node findMidDiagonalButton() {


? ? ? ? int rowDelta = GridPane.getRowIndex(first) - GridPane.getRowIndex(second);

? ? ? ? int colDelta = GridPane.getColumnIndex(first) - GridPane.getColumnIndex(second);


? ? ? ? if( Math.abs(rowDelta) != 2 ||? Math.abs(colDelta) != 2 ){

? ? ? ? ? ? Toolkit.getDefaultToolkit().beep();

? ? ? ? ? ? return null;

? ? ? ? }


? ? ? ? int rowsSum = GridPane.getRowIndex(first) + GridPane.getRowIndex(second);

? ? ? ? int colsSum = GridPane.getColumnIndex(first) + GridPane.getColumnIndex(second);


? ? ? ? return? getNodeByRowCol(Math.abs(rowsSum / 2), Math.abs(colsSum / 2) );

? ? }


? ? public Node getNodeByRowCol (int row, int col) {


? ? ? ? for (Node node : grid.getChildren()) {

? ? ? ? ? ? if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == col)

? ? ? ? ? ? ? ? return node;

? ? ? ? }


? ? ? ? return null;

? ? }


? ? public static void main(final String[] args) {

? ? ? ? launch(args);

? ? }

}

查看完整回答
反對(duì) 回復(fù) 2023-06-08
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

所以這是可能的,我從這篇文章中找到了答案。他只要自己想辦法,就能找到具體的位置。?javafx GridPane 檢索特定單元格內(nèi)容

private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {

for (Node node : gridPane.getChildren()) {

? ? if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {

? ? ? ? return node;

? ? }

}

return null;

}


雖然,為了我自己的目的,我仍然需要弄清楚如何能夠測(cè)試節(jié)點(diǎn)是否包含“紅色”或“黑色”,所以我只是添加了這個(gè),現(xiàn)在一切正常!


private Boolean getNodeFromGridPane(GridPane gridPane, int col, int row) {


? ? for (Node node : gridPane.getChildren()) {

? ? ? ? if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {


? ? ? ? ? ? if (node.toString().contains("black")) {


? ? ? ? ? ? ? ? System.out.println("The second button is black = " + node.toString().contains("black"));


? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? }


? ? ? ? ? ? if (node.toString().contains("red")) {


? ? ? ? ? ? ? ? System.out.println("The second button is red = " + node.toString().contains("red"));


? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? }


? ? ? ? }

? ? }

? ? return false;


}


查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 2 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)