Java,如何在GridLayout中水平和垂直交换组件的位置?

问题描述 投票:0回答:1

我已经完成了创建15个益智游戏的任务。如果空白磁贴水平或垂直在旁边,则玩家只能交换数字磁贴。我相信我的解决方案是创建Gridlayout 4x4。还有一个2d数组来查找特定Button的位置,这样我就可以在有意义的情况下彼此交换按钮的位置...

问题是当我使用panel.add(button0, row, col)时。它不像2d数组那样具有x和y位置,我认为确实如此。它导致按钮最终在错误的地方。

我如何才能使用目前所能解决的问题,还是必须重新开始?

提前感谢!

class GameTest extends JFrame implements ActionListener{

JPanel panel = new JPanel();
JButton[][] buttons = new JButton[4][4];
JButton button0 = new JButton("EMPTY");

public GameTest() {
    add(panel);
    setBackground(Color.RED);
    panel.setLayout(new GridLayout(4,4));
    int i = 1;
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++ ) {
            if (row == 3 && col == 3) {
                buttons[row][col] = button0;
                panel.add(buttons[row][col]);
                buttons[row][col].setBackground(Color.WHITE);
                buttons[row][col].setName("button0");
            }
            else{
                buttons[row][col] = new JButton(i + "");
                panel.add(buttons[row][col]);
                buttons[row][col].addActionListener(this);
                buttons[row][col].setBackground(Color.RED);
                buttons[row][col].setName("button" + i);
                buttons[row][col].setFont(new Font("Arial", Font.PLAIN, 30));
                i++;
            }
        }
    }
    setCursor(new Cursor(Cursor.HAND_CURSOR));
    setResizable(false);
    setLocation(500,200);
    setSize(400,400);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}


public boolean isSwappable(JButton button) {
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++ ) {
            if (buttons[row][col] == button) {
                if (row != 3 ) {
                    if (buttons[row+1][col] == button0){
                        return true;
                    }
                }
                else if (col != 3){
                    if (buttons[row][col+1] == button0){
                        return true;
                    }
                }
                else if (row != 0) {
                    if (buttons[row-1][col] == button0) {
                        return true;
                    }
                }
                else if (col != 0) {
                    if ( buttons[row][col-1] == button0){
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

@Override
public void actionPerformed(ActionEvent e) {
    JButton source = (JButton)e.getSource();
    if (isSwappable(source)) {
        int x = 0;
        int y = 0;
        JButton buttonTemp = null;
        JButton buttonTemp0 = null;
        for (int row = 0; row < buttons.length; row++) {
            for (int col = 0; col < buttons.length; col++) {
                if (buttons[row][col] == source) {
                    buttonTemp = source;

                    buttonTemp0 = button0;
                    x = row;
                    y = col;

                }
                if (buttons[row][col] == button0) {
                    buttons[row][col] = buttonTemp;
                    buttons[x][y] = buttonTemp0;
                }
            }
        }
    }
  }
}
java swing components swap grid-layout
1个回答
1
投票

[它不像2d数组那样具有x和y位置,

正确,它是一维数组。

因此您需要使用2D信息来计算索​​引。

例如,如果您想交换row = 1和column = 2的位置,则>

索引将是:

int index = (row * 4) + column;
© www.soinside.com 2019 - 2024. All rights reserved.