GridLayout上的移动限制仅限于相邻的网格按钮

时间:2014-05-10 22:27:50

标签: java swing jbutton point

我正在制作一个横跨jbuttons网格布局的游戏,只允许移动到相邻的网格jbuttons。目标是尝试使其成为topleft(从btm右侧)。我认为这一切都是相关的:

//这是我的方法,以确定它是否是一个有效的移动(我不希望你能够移动到任何地方,但相邻的点

    public boolean goodMove(int x, int y) {

    int val = Math.abs(current.x - x) + Math.abs(current.y - y);

    return val == 1;
}

//这是actionlister中的逻辑,一个大的if else循环,现在只引用gridlayout中的jbuttons(游戏面板)

      public void actionPerformed(ActionEvent e) {

    JButton clicked = (JButton) e.getSource();

    if (clicked == SHOWMINES) {

        if (SHOWMINES.getText().equals("Show Mines")) {
            showMines();
            SHOWMINES.setText("Hide Mines");
        } else {
            hideMines();
        }
    } else {
        if (clicked == NEWGAME) {
            newGame();
        } else {
            if (clicked == SHOWPATH) {
                if (SHOWPATH.getText().equals("Show Path")) {
                    showPath();
                    SHOWPATH.setText("Hide Path");
                } else {
                    hidePath();
                }
            } else {
                if (clicked == OKAY) {
                    resetGridSize(Integer.parseInt(gridSizeInput.getText()));
                    newGame();
                } else {

                }
                int gs = Integer.parseInt(gridSizeInput.getText());
                for (int i = 0; i < gs - 1; i++) {
                    for (int j = 0; j < gs - 1; j++) {
                        if (clicked == grid[i][j]) {
                            if (goodMove(i, j)) {//debug tester ,this is where the problem is.
                                System.out.println("TEST");
                                current = new Point(i, j);
                                grid[i][j].setBackground(Color.green);
                                grid[i][j].setText("=");
                            }else{System.out.println("TEST2");}
                        }
                    }
                }
            }
        }
    }
}// end of action listener

Grid [] []是我的网格按钮数组,gridSizeInput是可以调整网格大小的文本字段。

提前致谢(我是编程新手,所以如果可以的话,请保持简单:)

编辑:我忘了提及,它没有识别我的if(goodMove())循环,它只是为我的其他人打印出两个。

编辑:我的问题是为什么不工作,没有按钮会被识别为在我的网格中被点击,我想我想看看我写的东西是否明显错误。感谢。

编辑:好的,这是我所有按钮的整个动作监听器。这是我的第一篇文章,对不起,我没有更多的帮助 - 如果我能添加任何其他内容,请告诉我。

1 个答案:

答案 0 :(得分:2)

你的问题可能就像你没有将你的for循环扩展得足够简单一样简单。尝试更改此内容:

for (int i = 0; i < gs - 1; i++) {
    for (int j = 0; j < gs - 1; j++) {

到此:

for (int i = 0; i < gs; i++) {
    for (int j = 0; j < gs; j++) {

修改
这是我为测试这个概念而创建的最小代码示例:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;

import javax.swing.*;

public class Foo extends JPanel {
   public static final int GRID_SIZE = 10;
   private Point current;
   private JButton[][] grid;

   public Foo() {
      ButtonListener listener = new ButtonListener();
      setLayout(new GridLayout(GRID_SIZE, GRID_SIZE));
      grid = new JButton[GRID_SIZE][GRID_SIZE];
      for (int i = 0; i < grid.length; i++) {
         for (int j = 0; j < grid[i].length; j++) {
            grid[i][j] = new JButton("   ");
            grid[i][j].addActionListener(listener);
            add(grid[i][j]);
         }
      }

      current = new Point(GRID_SIZE - 1, GRID_SIZE - 1);
   }

   private class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         JButton clicked = (JButton) e.getSource();
         // !! int gs = Integer.parseInt(gridSizeInput.getText());
         int gs = GRID_SIZE;
         // !! for (int i = 0; i < gs - 1; i++) {
         // for (int j = 0; j < gs - 1; j++) {
         for (int i = 0; i < gs; i++) {
            for (int j = 0; j < gs; j++) {

               if (clicked == grid[i][j]) {
                  if (goodMove(i, j)) {
                     System.out.println("TEST");
                     current = new Point(i, j);
                     grid[i][j].setBackground(Color.green);
                     grid[i][j].setText("=");
                  } else {
                     System.out.println("TEST2");
                  }
               }
            }
         }
      }
   }

   public boolean goodMove(int x, int y) {
      int val = Math.abs(current.x - x) + Math.abs(current.y - y);
      return val == 1;
   }

   private static void createAndShowGui() {
      Foo mainPanel = new Foo();
      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}