当我单击按钮时,我想在我设置的范围内移动

基德城

此代码旨在移动屏幕截图中显示的图像。

我想要做的是当我点击按钮时,它应该像

下棋的骑士。它根本不动,显示以下错误。

这里有什么问题?我上传了我的代码。

感谢您的帮助和考虑

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;




public class BoardClass extends JFrame{


    private Container contents;

    private JButton[][] squares = new JButton[8][8];
    private Color colorBlack = Color.BLACK;
    private Color colorOrange = Color.ORANGE;

    private int sheeprow=7;
    private int sheepcol=0;

    private int woolfrow1=0;
    private int woolfcol1=1;
    private int woolfrow2=0;
    private int woolfcol2=3;
    private int woolfrow3=0;
    private int woolfcol3=5;
    private int woolfrow4=0;
    private int woolfcol4=7;

    private ImageIcon sheep = new ImageIcon("sheep.PNG");
    private ImageIcon wolf = new ImageIcon("wolf.PNG");

    public BoardClass()
    {
        super("Sheep and wolf game"); 

        contents = getContentPane();
        contents.setLayout(new GridLayout(8, 8));

        ButtonHandler buttonhandler = new ButtonHandler();

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


                squares[i][j] = new JButton();
                if((i+j)%2!=0) // it allows to show black blocks
                {
                    squares[i][j].setBackground(colorBlack);
                }
                else
                {
                    squares[i][j].setBackgroun(colorOrange);
                }
                contents.add(squares[i][j]);
                squares[i][j].addActionListener(buttonhandler);
            }
        }
        squares[sheeprow][sheepcol].setIcon(sheep);
        /*squares[woolfrow1][woolfcol1].setIcon(wolf);
        squares[woolfrow2][woolfcol2].setIcon(wolf);
        squares[woolfrow3][woolfcol3].setIcon(wolf);
        squares[woolfrow4][woolfcol4].setIcon(wolf);*/

        setSize(500,500);
        setResizable(false);
        setLocationRelativeTo(null); //centers window
        setVisible(true);

    }


    private boolean isValidMove(int i, int j)
    {
        int rowDelta = Math.abs(i-sheeprow);
        int colDelta = Math.abs(j-sheepcol);

        if((rowDelta==1) && (colDelta==2))
        {
            return true;
        }
        if((colDelta==1)&&(rowDelta==2))
        {
            return true;
        }
        return false;
    }

    private void processClick(int i, int j)
    {
        if(isValidMove(i,j)==false)
        {
            return;
        }
        squares[sheeprow][sheepcol].setIcon(null);
        squares[i][j].setIcon(sheep);
        sheeprow = i;
        sheepcol = j;
    }

private class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();

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

                if(source==squares[i][j])-- this part showing some error
                {
                    processClick(i,j);
                    return;
                }
            }
        }

    }
}

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new BoardClass();
    }
}

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

雅克

actionPerformed你需要改变行

for(int j=0;i<8;j++)

for(int j=0;j<8;j++)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我想在单击按钮时更新表

我想在段落中显示图像,但是当我单击以显示图像时,文本会上下移动

我想在每次单击按钮时创建组合框

我想在单击 eventlistner 按钮时刷新复选框

当我单击Android日历列表视图中的重置按钮时如何清除选定的日期范围

当我单击链接时,为什么内盒会移动?

当我指定一个 y 范围时,图形在该范围内无法正确显示

当我单击并拖动鼠标时如何使Jframe移动

当我单击列表项时如何设置单选按钮以选中

当我单击按钮内的元素时,onClick函数不获取值

当我使用js单击按钮时如何隐藏表格?

当我单击按钮时,应用程序停止

当我单击注销按钮时弹出弹出窗口

当我尝试单击按钮时,它不起作用

当我单击动态按钮时的动态模态

当我在 PageView 中单击按钮时如何转到页面?

当我单击按钮打开此活动时,它关闭

当我单击按钮而不是UIAlertcontroller时创建动作

当我在reactjs中单击按钮时如何加载函数

我想在java中生成给定范围内的随机数

当我在弹出窗口之外单击时如何设置动作?

当我在另一个QWidget中按下按钮时,我想在QPixmap中设置path_to_photo

使用UI Sortable,当我单击按钮时,是否可以根据给定的值将项目移动到新位置?

当我单击其他按钮(如德西美食)时,我的订购按钮被触发

当我单击按钮时,应该将按钮ID返回给我。JSON文件

我在表单中使用了日期范围选择器,当我单击日期范围选择器的应用按钮时,如何在 jquery 中获取更改的日期

当我单击TabLayout的选项卡或按viewpager移动时,我想加载片段

我想在我的viewpager页面中为按钮设置onClickListener

单击按钮并再次单击时我需要一个popupview我想在目标c中分解该视图