首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX在网格窗格上交换按钮

JavaFX在网格窗格上交换按钮
EN

Stack Overflow用户
提问于 2019-06-09 22:30:02
回答 1查看 592关注 0票数 1

我正在用JavaFX写一个益智游戏。

您可以在单击网格窗格上的按钮后将其与其他按钮互换吗?

例如。我想在按下按钮1之后交换一下1号和9号按钮。我想应该可以在网格面板上交换位置。

例如,当我点击按钮4时,它与按钮9互换。

有办法做到这一点吗?

下面是我的代码:

代码语言:javascript
复制
import javafx.application.Application;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

import javafx.scene.Parent;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javafx.scene.image.ImageView;


import javafx.scene.image.Image;

import javafx.embed.swing.SwingFXUtils;
import javafx.scene.layout.GridPane;

import javafx.geometry.Insets;



public class Main extends Application {

    static Scene scene1;

    @Override
    public void start(Stage primaryStage) throws Exception{

        primaryStage.setTitle("Puzzles");
        Label label1= new Label("Let's play a game");
        Button button1= new Button("Go back to the menu");
        Parent root = FXMLLoader.load(getClass().getResource("First.fxml"));
        Scene scene = new Scene(root);
        button1.setOnAction(e -> primaryStage.setScene(scene));



        GridPane gridPane = new GridPane();

        gridPane.setMinSize(800, 600);

        gridPane.setPadding(new Insets(10, 10, 10, 10));




        BufferedImage originalImgage = ImageIO.read(new File("C:\\Users\\PC\\Desktop\\mateusz\\Project2\\src\\pic.jpg"));
        BufferedImage blank = ImageIO.read(new File("C:\\Users\\PC\\Desktop\\mateusz\\Project2\\src\\bialt.png"));


        BufferedImage one = originalImgage.getSubimage(0, 0, 100, 100);
        BufferedImage two = originalImgage.getSubimage(100, 0, 100, 100);


        Image q1 = SwingFXUtils.toFXImage(one, null );
        Image q2 = SwingFXUtils.toFXImage(two, null );

        Image q9 = SwingFXUtils.toFXImage(blank, null );

        ImageView i1 = new ImageView(q1);
        ImageView i2 = new ImageView(q2);

        ImageView i9 = new ImageView(q9);


        Button b1 = new Button("",i1);
        Button b2 = new Button("",i2);

        Button b9 = new Button("",i9);


        gridPane.add(label1, 0, 0);
        gridPane.add(button1, 1, 0);
        gridPane.add(b1, 0, 1);
        gridPane.add(b2, 1, 1);
        gridPane.add(b9, 2, 3);



        scene1= new Scene(gridPane, 800, 600);


        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-10 00:04:50

您可以通过交换两个节点的行/列索引来交换GridPane的两个子节点的“单元”:

代码语言:javascript
复制
public static void swap(Node n1, Node n2) {
    Integer temp = GridPane.getRowIndex(n1);
    GridPane.setRowIndex(n1, GridPane.getRowIndex(n2));
    GridPane.setRowIndex(n2, temp);

    temp = GridPane.getColumnIndex(n1);
    GridPane.setColumnIndex(n1, GridPane.getColumnIndex(n2));
    GridPane.setColumnIndex(n2, temp);
}

用法:

代码语言:javascript
复制
swap(b1, b9); // change places of b1 and b9
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56515434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档