繁体   English   中英

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

[英]Movement restriction on GridLayout to only adjacent grid buttons

我正在制作一个在jbutton的网格布局上移动,只允许移动到相邻网格jbutton的游戏。 目标是尝试使其移至左上角(从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(游戏面板)中的jbutton

      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是可以在其中调整gridsize的文本字段。

在此先感谢您(我是编程新手,请尽量保持简单:)

编辑:我忘了提一下,它无法识别我的if(goodMove())循环,它只是为我的其他打印了两个。

编辑:我的问题是为什么不起作用,在网格中没有按钮会被识别为单击,我想我想问一问我写的东西是否明显地错了。 谢谢。

编辑:好的,这是我所有按钮的整个动作监听器。 这是我的第一篇帖子,对不起,我没有更多帮助-如果我可以添加其他信息,请告诉我。

您的问题可能很简单,例如您没有将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();
         }
      });
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM