【问题标题】:Java, How to swap position of components horizontally and vertically in a GridLayout?Java,如何在 GridLayout 中水平和垂直交换组件的位置?
【发布时间】:2020-02-22 03:27:12
【问题描述】:

我接到了一项任务,要创建一个 15 人的益智游戏。如果空白瓷砖水平或垂直靠近它,玩家只能交换数字瓷砖。我相信我的解决方案是创建一个 Gridlayout 4x4。还有一个二维数组来查找特定按钮的位置,这样我就可以在有意义的情况下相互交换按钮的位置......

问题是当我使用panel.add(button0, row, col) 时。它没有像二维数组那样的 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】:

    它不像二维数组那样有 x 和 y 位置,

    没错,它是一维数组。

    所以你需要使用二维信息来计算索​​引。

    例如,如果您想交换行 = 1 和列 = 2 的位置

    索引将是:

    int index = (row * 4) + column;
    

    【讨论】:

    • 添加/删除组件后,您需要重新验证()面板,以便布局管理器可以设置每个组件的大小/位置。
    • 谢谢!我设法解决了。需要先将组件添加到较低的索引,否则您将获得非法组件位置。我注意到的另一个问题是方法 isSwappable...逻辑关闭...它几乎只向上交换而不是向侧面或再次向下交换...
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多