Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2x2棋盘_Java_Swing_Awt - Fatal编程技术网

Java 2x2棋盘

Java 2x2棋盘,java,swing,awt,Java,Swing,Awt,我在写一个2x2的棋盘。我的代码在我按下的地方工作,它会下降,但如果我在底部网格中,我想禁用这个按钮,我怎么做,因为我想禁用某些网格的某些按钮,但不是所有网格的相同按钮 import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class assignment1 extends JFrame implement

我在写一个2x2的棋盘。我的代码在我按下的地方工作,它会下降,但如果我在底部网格中,我想禁用这个按钮,我怎么做,因为我想禁用某些网格的某些按钮,但不是所有网格的相同按钮

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


public class assignment1 extends JFrame implements ActionListener  {

        JPanel board, buttons;

        JLabel G1, G2, G3, G4;

        JButton B1, B2, B3, B4, B5, B6, B7, B8, B9;

        ImageIcon i1 = new ImageIcon("Images/pawn.jpg");
        ImageIcon a1 = new ImageIcon("Images/left_up.png");
        ImageIcon a2 = new ImageIcon("Images/up.png");
        ImageIcon a3 = new ImageIcon("Images/right_up.png");
        ImageIcon a4 = new ImageIcon("Images/left.png");
        ImageIcon a5 = new ImageIcon("Images/reset.png");
        ImageIcon a6 = new ImageIcon("Images/right.png");
        ImageIcon a7 = new ImageIcon("Images/left_down.png");
        ImageIcon a8 = new ImageIcon("Images/down.png");
        ImageIcon a9 = new ImageIcon("Images/right_down.png");

    public static void main(String[]args){

        assignment1 frame = new assignment1();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public assignment1() {

        super("Chess Board");

                Container c = getContentPane();
                c.setLayout(new GridLayout (2,3));

            board = new JPanel();
            board.setLayout(new GridLayout(2,2,4,4));
            JPanel buttons = new JPanel();
            buttons.setLayout(new GridLayout(3,3,2,2));

            G1 = new JLabel(i1);
            G1.setBackground(Color.white);
            G1.setOpaque(true);
            G1.setIcon(i1);

            board.add(G1);

            G2 = new JLabel(i1);
            G2.setBackground(Color.black);
            G2.setOpaque(true);
            G2.setIcon(null);

            board.add(G2);

            G3 =  new JLabel(i1);
            G3.setBackground(Color.black);
            G3.setOpaque(true);
            G3.setIcon(null);

            board.add(G3);

            G4 = new JLabel(i1);
            G4.setBackground(Color.white);
            G4.setOpaque(true);
            G4.setIcon(null);

            board.add(G4);

            c.add(board);
            //c.add(board.BorderLayout.NORTH);
            c.add(buttons);

            B1 = new JButton(a1);
            buttons.add(B1);

            B2 = new JButton(a2);
            buttons.add(B2);

            B3 = new JButton(a3);
            buttons.add(B3);

            B4 = new JButton(a4);
            buttons.add(B4);

            B5 = new JButton(a5);
            buttons.add(B5);

            B6 = new JButton(a6);
            buttons.add(B6);

            B7 = new JButton(a7);
            buttons.add(B7);

            B8 = new JButton(a8);
            buttons.add(B8);

            B9 = new JButton(a9);
            buttons.add(B9);

            B1.addActionListener(this);
            B2.addActionListener(this);
            B3.addActionListener(this);
            B4.addActionListener(this);
            B5.addActionListener(this);
            B6.addActionListener(this);
            B7.addActionListener(this);
            B8.addActionListener(this);
            B9.addActionListener(this);

            setVisible(true);
            setSize(500,500);

    }
            public void actionPerformed(ActionEvent e) {


                //if statement for Grid 1 Button 6 Right
                if (G1.getIcon() == i1)
                {
                    if(e.getSource() == B6)
                    {
                        G1.setIcon(null);
                        G2.setIcon(i1);
                    }
                }

                //if statement for Grid 1 Button 9 Right_down
                if (G1.getIcon() == i1)
                {
                    if(e.getSource() == B9)
                    {
                        G1.setIcon(null);
                        G4.setIcon(i1);
                    }
                }

                //if statement for Grid 1 Button 8 Down
                if (G1.getIcon() == i1)
                {
                    if(e.getSource() == B8)
                    {
                        G1.setIcon(null);
                        G3.setIcon(i1);
                    }
                }

                //if statement for Grid 2 Button 4 Left
                if(G2.getIcon() == i1)
                {
                    if(e.getSource() == B4)
                    {
                        G2.setIcon(null);
                        G1.setIcon(i1);
                    }
                }

                //if statement for Grid 2 Button 7 left Down
                if(G2.getIcon() == i1)
                {
                    if(e.getSource() == B7)
                    {
                        G2.setIcon(null);
                        G3.setIcon(i1);
                    }
                }

                //if statement for Grid 2 Button 8 Down
                if(G2.getIcon() == i1)
                {
                    if(e.getSource() == B8)
                    {
                        G2.setIcon(null);
                        G4.setIcon(i1);
                    }
                }

                //if statement for Grid 3 Button 2 Up
                if(G3.getIcon() == i1)
                {
                    if(e.getSource() == B2)
                    {
                        G3.setIcon(null);
                        G1.setIcon(i1);
                    }
                }

                //if statement for Grid 3 Button 6 right
                if(G3.getIcon() == i1)
                {
                    if(e.getSource() == B6)
                    {
                        G3.setIcon(null);
                        G4.setIcon(i1);
                    }
                }

                //if statement for Grid 3 Button 3 right Up
                if(G3.getIcon() == i1)
                {
                    if(e.getSource() == B3)
                    {
                        G3.setIcon(null);
                        G2.setIcon(i1);
                    }
                }

                //if statement for Grid 4 Button 2 Up
                if(G4.getIcon() == i1)
                {
                    if(e.getSource() == B2)
                    {
                        G4.setIcon(null);
                        G2.setIcon(i1);
                    }
                }

                //if statement for Grid 4 Button 4 Left
                if(G4.getIcon() == i1)
                {
                    if(e.getSource() == B4)
                    {
                        G4.setIcon(null);
                        G3.setIcon(i1);
                    }
                }

                //if statement for Grid 4 Button 1 Left Up
                if(G4.getIcon() == i1)
                {

                    if(e.getSource() == B1)
                    {
                        G4.setIcon(null);
                        G1.setIcon(i1);
                    }
                }

                //reset Button
                if (e.getSource() == B5)
                {
                    G1.setIcon(i1);
                    G2.setIcon(null);
                    G3.setIcon(null);
                    G4.setIcon(null);
                }

            }
        }

添加到动作执行功能:在每次动作前检查当前位置,以便在按下f/e键时检查位置是否位于左下角。按钮2什么都不应该发生。
或者我没听清你的问题

你记下典当的位置

 int x=0, y=0;
当你移动典当时,你检查x是否=1,如果是,那么你禁用右手按钮,如果它==0,那么你禁用左手按钮。然后你对y也这样做

只要确保每次移动时都更新它们

 //change x and y based on button clicked

 if (x==1)
 {
  //disable right buttons & enable left
 }
 else if (x==0)
 {
 //disable left buttons & enable right
 }

 if (y==1)
 {
  //disable down buttons & enable up
 }
 else if (y==0)
 {
 //disable up buttons & enable down
 }
我想为某些网格禁用某些按钮,但不一样 所有网格的按钮

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


public class assignment1 extends JFrame implements ActionListener  {

        JPanel board, buttons;

        JLabel G1, G2, G3, G4;

        JButton B1, B2, B3, B4, B5, B6, B7, B8, B9;

        ImageIcon i1 = new ImageIcon("Images/pawn.jpg");
        ImageIcon a1 = new ImageIcon("Images/left_up.png");
        ImageIcon a2 = new ImageIcon("Images/up.png");
        ImageIcon a3 = new ImageIcon("Images/right_up.png");
        ImageIcon a4 = new ImageIcon("Images/left.png");
        ImageIcon a5 = new ImageIcon("Images/reset.png");
        ImageIcon a6 = new ImageIcon("Images/right.png");
        ImageIcon a7 = new ImageIcon("Images/left_down.png");
        ImageIcon a8 = new ImageIcon("Images/down.png");
        ImageIcon a9 = new ImageIcon("Images/right_down.png");

    public static void main(String[]args){

        assignment1 frame = new assignment1();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public assignment1() {

        super("Chess Board");

                Container c = getContentPane();
                c.setLayout(new GridLayout (2,3));

            board = new JPanel();
            board.setLayout(new GridLayout(2,2,4,4));
            JPanel buttons = new JPanel();
            buttons.setLayout(new GridLayout(3,3,2,2));

            G1 = new JLabel(i1);
            G1.setBackground(Color.white);
            G1.setOpaque(true);
            G1.setIcon(i1);

            board.add(G1);

            G2 = new JLabel(i1);
            G2.setBackground(Color.black);
            G2.setOpaque(true);
            G2.setIcon(null);

            board.add(G2);

            G3 =  new JLabel(i1);
            G3.setBackground(Color.black);
            G3.setOpaque(true);
            G3.setIcon(null);

            board.add(G3);

            G4 = new JLabel(i1);
            G4.setBackground(Color.white);
            G4.setOpaque(true);
            G4.setIcon(null);

            board.add(G4);

            c.add(board);
            //c.add(board.BorderLayout.NORTH);
            c.add(buttons);

            B1 = new JButton(a1);
            buttons.add(B1);

            B2 = new JButton(a2);
            buttons.add(B2);

            B3 = new JButton(a3);
            buttons.add(B3);

            B4 = new JButton(a4);
            buttons.add(B4);

            B5 = new JButton(a5);
            buttons.add(B5);

            B6 = new JButton(a6);
            buttons.add(B6);

            B7 = new JButton(a7);
            buttons.add(B7);

            B8 = new JButton(a8);
            buttons.add(B8);

            B9 = new JButton(a9);
            buttons.add(B9);

            B1.addActionListener(this);
            B2.addActionListener(this);
            B3.addActionListener(this);
            B4.addActionListener(this);
            B5.addActionListener(this);
            B6.addActionListener(this);
            B7.addActionListener(this);
            B8.addActionListener(this);
            B9.addActionListener(this);

            setVisible(true);
            setSize(500,500);

    }
            public void actionPerformed(ActionEvent e) {


                //if statement for Grid 1 Button 6 Right
                if (G1.getIcon() == i1)
                {
                    if(e.getSource() == B6)
                    {
                        G1.setIcon(null);
                        G2.setIcon(i1);
                    }
                }

                //if statement for Grid 1 Button 9 Right_down
                if (G1.getIcon() == i1)
                {
                    if(e.getSource() == B9)
                    {
                        G1.setIcon(null);
                        G4.setIcon(i1);
                    }
                }

                //if statement for Grid 1 Button 8 Down
                if (G1.getIcon() == i1)
                {
                    if(e.getSource() == B8)
                    {
                        G1.setIcon(null);
                        G3.setIcon(i1);
                    }
                }

                //if statement for Grid 2 Button 4 Left
                if(G2.getIcon() == i1)
                {
                    if(e.getSource() == B4)
                    {
                        G2.setIcon(null);
                        G1.setIcon(i1);
                    }
                }

                //if statement for Grid 2 Button 7 left Down
                if(G2.getIcon() == i1)
                {
                    if(e.getSource() == B7)
                    {
                        G2.setIcon(null);
                        G3.setIcon(i1);
                    }
                }

                //if statement for Grid 2 Button 8 Down
                if(G2.getIcon() == i1)
                {
                    if(e.getSource() == B8)
                    {
                        G2.setIcon(null);
                        G4.setIcon(i1);
                    }
                }

                //if statement for Grid 3 Button 2 Up
                if(G3.getIcon() == i1)
                {
                    if(e.getSource() == B2)
                    {
                        G3.setIcon(null);
                        G1.setIcon(i1);
                    }
                }

                //if statement for Grid 3 Button 6 right
                if(G3.getIcon() == i1)
                {
                    if(e.getSource() == B6)
                    {
                        G3.setIcon(null);
                        G4.setIcon(i1);
                    }
                }

                //if statement for Grid 3 Button 3 right Up
                if(G3.getIcon() == i1)
                {
                    if(e.getSource() == B3)
                    {
                        G3.setIcon(null);
                        G2.setIcon(i1);
                    }
                }

                //if statement for Grid 4 Button 2 Up
                if(G4.getIcon() == i1)
                {
                    if(e.getSource() == B2)
                    {
                        G4.setIcon(null);
                        G2.setIcon(i1);
                    }
                }

                //if statement for Grid 4 Button 4 Left
                if(G4.getIcon() == i1)
                {
                    if(e.getSource() == B4)
                    {
                        G4.setIcon(null);
                        G3.setIcon(i1);
                    }
                }

                //if statement for Grid 4 Button 1 Left Up
                if(G4.getIcon() == i1)
                {

                    if(e.getSource() == B1)
                    {
                        G4.setIcon(null);
                        G1.setIcon(i1);
                    }
                }

                //reset Button
                if (e.getSource() == B5)
                {
                    G1.setIcon(i1);
                    G2.setIcon(null);
                    G3.setIcon(null);
                    G4.setIcon(null);
                }

            }
        }
GridLayout
具有组件的
从左到右的方向它按照您添加的顺序布局组件。因此,对于
2x2
网格:

 jPanel1.setLayout(new GridLayout(2, 2));

 jPanel1.add(button1); //button1 is at grid (1, 1)
 jPanel1.add(button2); //button2 is at grid (1, 2)
 jPanel1.add(button3); //button3 is at grid (2, 1)
 jPanel1.add(button4); //button4 is at grid (2, 2) 
因此,要在网格单元
(行,列)
中找到一个组件并使用它,实际上很容易: 首先,找到组件的索引,然后要求
jPanel1
返回它:

GridLayout layout = (GridLayout) jPanel1.getLayout();
int numCol = layout.getColumns();
int index = (row-1)*numCol + col-1; 

if(index < jPanel1.getComponentCount())
    jPanel1.getComponent(index).setEnabled(false);
GridLayout布局=(GridLayout)jPanel1.getLayout();
int numcoll=layout.getColumns();
整数索引=(第1行)*numCol+col-1;
if(索引
好的,我知道我做这个有点晚了,我可能应该比现在早一点回来,但我想分享我的代码,以防有其他人想做一个棋盘,我只做了一个2x2,但你可能可以从这个扩展

//importing java swing and awt to help program work
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


    public class assignment1 extends JFrame implements ActionListener  {

            //declaring the JPanel, JLabel and JButton's Globally
            JPanel board, buttons;

            JLabel grid1, grid2, grid3, grid4;

            JButton button1, button2, button3, button4, button5, button6, button7, button8, button9;

            //Declaring images globally
            ImageIcon pawn = new ImageIcon("Images/pawn.png");
            ImageIcon arrow1 = new ImageIcon("Images/left_up.png");
            ImageIcon arrow2 = new ImageIcon("Images/up.png");
            ImageIcon arrow3 = new ImageIcon("Images/right_up.png");
            ImageIcon arrow4 = new ImageIcon("Images/left.png");
            ImageIcon arrow5 = new ImageIcon("Images/reset.png");
            ImageIcon arrow6 = new ImageIcon("Images/right.png");
            ImageIcon arrow7 = new ImageIcon("Images/left_down.png");
            ImageIcon arrow8 = new ImageIcon("Images/down.png");
            ImageIcon arrow9 = new ImageIcon("Images/right_down.png");

        public static void main(String[]args){

            assignment1 frame = new assignment1();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public assignment1() {

            super("Chess Board");

                    Container c = getContentPane();
                    c.setLayout(new GridLayout (2,2));

                //setting up new JPanel for board to display the 4 grids
                board = new JPanel();
                board.setLayout(new GridLayout(2,2,4,4));
                JPanel buttons = new JPanel();
                buttons.setLayout(new GridLayout(3,3,2,2));

                //setting up grid1
                grid1 = new JLabel(pawn);//placing the image in the background of the grid
                grid1.setBackground(Color.white);
                grid1.setOpaque(true);
                grid1.setIcon(pawn);//displaying image

                //adding grid1 to the board
                board.add(grid1);

                //setting up grid2
                grid2 = new JLabel(pawn);//placing the image in the background of the grid
                grid2.setBackground(Color.black);
                grid2.setOpaque(true);
                grid2.setIcon(null);//setting icon to null

                //adding grid2 to the board
                board.add(grid2);

                //setting up grid3
                grid3 =  new JLabel(pawn);//placing the image in the background of the grid
                grid3.setBackground(Color.black);
                grid3.setOpaque(true);
                grid3.setIcon(null);//setting icon to null

                //adding grid3 to the board
                board.add(grid3);

                //setting up grid4
                grid4 = new JLabel(pawn);//placing the image in the background of the grid
                grid4.setBackground(Color.white);
                grid4.setOpaque(true);
                grid4.setIcon(null);//setting icon to null

                //adding grid4 to the board
                board.add(grid4);

                //adding board to the container
                c.add(board);
                //adding buttons to the container
                c.add(buttons);

                //Creating button 1 and adding an image to Button 1
                button1 = new JButton(arrow1);
                buttons.add(button1);
                button1.setEnabled(false);//hard setting the button so that the start up will disable this button

                //Creating button 2 and adding an image to Button 2
                button2 = new JButton(arrow2);
                buttons.add(button2);
                button2.setEnabled(false);//hard setting the button so that the start up will disable this button

                //Creating button 3 and adding an image to Button 3
                button3 = new JButton(arrow3);
                buttons.add(button3);
                button3.setEnabled(false);//hard setting the button so that the start up will disable this button

                //Creating button 4 and adding an image to Button 4
                button4 = new JButton(arrow4);
                buttons.add(button4);
                button4.setEnabled(false);//hard setting the button so that the start up will disable this button

                //Creating button 5 and adding an image to Button 5
                button5 = new JButton(arrow5);
                buttons.add(button5);
                button5.setEnabled(false);//hard setting the button so that the start up will disable this button

                //Creating button 6 and adding an image to Button 6
                button6 = new JButton(arrow6);
                buttons.add(button6);
                button6.setEnabled(true);//hard setting the button so that the start up will enable this button

                //Creating button 7 and adding an image to Button 7
                button7 = new JButton(arrow7);
                buttons.add(button7);
                button7.setEnabled(false);//hard setting the button so that the start up will disable this button

                //Creating button 8 and adding an image to Button 8
                button8 = new JButton(arrow8);
                buttons.add(button8);
                button8.setEnabled(true);//hard setting the button so that the start up will enable this button

                //Creating button 9 and adding an image to Button 9
                button9 = new JButton(arrow9);
                buttons.add(button9);
                button9.setEnabled(true);//hard setting the button so that the start up will enable this button

                //method for button 1
                button1.addActionListener( new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonOne)
                {
                    firstButtonPerformed(buttonOne);
                }
            });

                //method for button 2
                button2.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonTwo)
                {
                    secondButtonPerformed(buttonTwo);
                }
            });
                //method for button 3
                button3.addActionListener(new ActionListener()
                {
                public void actionPerformed(ActionEvent buttonThree)
                                {
                                    thirdButtonPerformed(buttonThree);
                                }
            });
                //method for button 4
                button4.addActionListener(new ActionListener()
                {
                public void actionPerformed(ActionEvent buttonFour)
                                {
                                    fourthButtonPerformed(buttonFour);
                                }
            });
                //method for button 5
                button5.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonFive)
                {
                    fifthButtonPerformed(buttonFive);
                }
            });
                //method for button 6
                button6.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonSix)
                {
                    sixthButtonPerformed(buttonSix);
                }
            });
                //method for button 7
                button7.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonSeven)
                {
                    seventhButtonPerformed(buttonSeven);
                }
            });
                //method for button 8
                button8.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonEight)
                {
                    eightButtonPerformed(buttonEight);
                }
            });
                //method for button 9
                button9.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent buttonNine)
                {
                    ninethButtonPerformed(buttonNine);
                }
            });


                setVisible(true);
                setSize(500,500);

        }
            public void firstButtonPerformed(ActionEvent buttonOne)

            {
                //if statement for Grid 4 Button 1 Left Up
                if(grid4.getIcon() == pawn)
                {
                    if(buttonOne.getSource() == button1)
                        {
                            grid4.setIcon(null);
                            grid1.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(false);
                            button3.setEnabled(false);
                            button4.setEnabled(false);
                            button5.setEnabled(false);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(true);
                            button9.setEnabled(true);
                        }
                    }//end of if statement
                }

                public void secondButtonPerformed(ActionEvent buttonTwo)
                {
                    //if statement for Grid 3 Button 2 Up
                    if(grid3.getIcon() == pawn)
                    {
                        if(buttonTwo.getSource() == button2)
                        {
                            grid3.setIcon(null);
                            grid1.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(false);
                            button3.setEnabled(false);
                            button4.setEnabled(false);
                            button5.setEnabled(false);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(true);
                            button9.setEnabled(true);
                        }
                    }//end of if statement

                    //start of else if statement for button 2 for grid 2
                    else if(grid4.getIcon() == pawn)
                    {
                        if(buttonTwo.getSource() == button2)
                        {
                            grid4.setIcon(null);
                            grid2.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(false);
                            button3.setEnabled(false);
                            button4.setEnabled(false);
                            button5.setEnabled(true);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(true);
                            button9.setEnabled(true);
                        }
                    }//end of if statement
                }

            public void thirdButtonPerformed(ActionEvent buttonThree)
            {
                //if statement for Grid 3 Button 3 right Up
                    if(grid3.getIcon() == pawn)
                    {
                        if(buttonThree.getSource() == button3)
                        {
                            grid3.setIcon(null);
                            grid2.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(false);
                            button3.setEnabled(false);
                            button4.setEnabled(true);
                            button5.setEnabled(true);
                            button6.setEnabled(false);
                            button7.setEnabled(true);
                            button8.setEnabled(true);
                            button9.setEnabled(false);
                        }
                    }//end of if statement
            }

            public void fourthButtonPerformed(ActionEvent buttonFour)
            {
                //if statement for Grid 2 Button 4 Left
                    if(grid2.getIcon() == pawn)
                    {
                        if(buttonFour.getSource() == button4)
                        {
                            grid2.setIcon(null);
                            grid1.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(false);
                            button3.setEnabled(false);
                            button4.setEnabled(false);
                            button5.setEnabled(false);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(true);
                            button9.setEnabled(true);
                        }
                    }//end of if statement

                    //start of else if statement for button4 for grid 3
                    else if(grid4.getIcon() == pawn)
                    {
                        if(buttonFour.getSource() == button4)
                        {
                            grid4.setIcon(null);
                            grid3.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(true);
                            button3.setEnabled(true);
                            button4.setEnabled(false);
                            button5.setEnabled(true);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(false);
                            button9.setEnabled(false);
                        }
                    }//end of else if statement
            }

            public void fifthButtonPerformed(ActionEvent buttonFive)
            {
                //reset Button if statement
                    if (buttonFive.getSource() == button5)
                    {
                        grid1.setIcon(pawn);
                        grid2.setIcon(null);
                        grid3.setIcon(null);
                        grid4.setIcon(null);

                        button1.setEnabled(false);
                        button2.setEnabled(false);
                        button3.setEnabled(false);
                        button4.setEnabled(false);
                        button5.setEnabled(false);
                        button6.setEnabled(true);
                        button7.setEnabled(false);
                        button8.setEnabled(true);
                        button9.setEnabled(true);
                    }//end of reset button if statement
            }

            public void sixthButtonPerformed(ActionEvent buttonSix)
            {
                //if statement for Grid 1 Button 6 Right
                    if (grid1.getIcon() == pawn)
                    {
                        if(buttonSix.getSource() == button6)
                        {
                            grid1.setIcon(null);
                            grid2.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(false);
                            button3.setEnabled(false);
                            button4.setEnabled(true);
                            button5.setEnabled(true);
                            button6.setEnabled(false);
                            button7.setEnabled(true);
                            button8.setEnabled(true);
                            button9.setEnabled(false);
                        }
                    }//end of if statement

                    //start of else if statement for button6 for grid4
                    else if (grid3.getIcon() == pawn)
                    {
                        if(buttonSix.getSource() == button6)
                        {
                            grid3.setIcon(null);
                            grid4.setIcon(pawn);

                            button1.setEnabled(true);
                            button2.setEnabled(true);
                            button3.setEnabled(false);
                            button4.setEnabled(true);
                            button5.setEnabled(true);
                            button6.setEnabled(false);
                            button7.setEnabled(false);
                            button8.setEnabled(false);
                            button9.setEnabled(false);
                        }
                    }//end of else if statement
            }

            public void seventhButtonPerformed(ActionEvent buttonSeven)
            {
                //if statement for Grid 2 Button 7 left Down
                    if(grid2.getIcon() == pawn)
                    {
                        if(buttonSeven.getSource() == button7)
                        {
                            grid2.setIcon(null);
                            grid3.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(true);
                            button3.setEnabled(true);
                            button4.setEnabled(false);
                            button5.setEnabled(true);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(false);
                            button9.setEnabled(false);
                        }
                    }//end of if statement
            }

            public void eightButtonPerformed(ActionEvent buttonEight)
            {
                //if statement for Grid 1 Button 8 Down
                    if (grid1.getIcon() == pawn)
                    {
                        if(buttonEight.getSource() == button8)
                        {
                            grid1.setIcon(null);
                            grid3.setIcon(pawn);

                            button1.setEnabled(false);
                            button2.setEnabled(true);
                            button3.setEnabled(true);
                            button4.setEnabled(false);
                            button5.setEnabled(true);
                            button6.setEnabled(true);
                            button7.setEnabled(false);
                            button8.setEnabled(false);
                            button9.setEnabled(false);
                        }
                    }//end of if statement

                    //start of else if statement for button 8 grid4
                    else if (grid2.getIcon() == pawn)
                    {
                        if(buttonEight.getSource() == button8)
                        {
                            grid2.setIcon(null);
                            grid4.setIcon(pawn);

                            button1.setEnabled(true);
                            button2.setEnabled(true);
                            button3.setEnabled(false);
                            button4.setEnabled(true);
                            button5.setEnabled(true);
                            button6.setEnabled(false);
                            button7.setEnabled(false);
                            button8.setEnabled(false);
                            button9.setEnabled(false);
                        }
                    }//end of else if statement
            }

            public void ninethButtonPerformed(ActionEvent buttonNine)
            {
                //if statement for Grid 1 Button 9 Right_down
                    if (grid1.getIcon() == pawn)
                    {
                        if(buttonNine.getSource() == button9)
                        {
                            grid1.setIcon(null);
                            grid4.setIcon(pawn);

                            button1.setEnabled(true);
                            button2.setEnabled(true);
                            button3.setEnabled(false);
                            button4.setEnabled(true);
                            button5.setEnabled(true);
                            button6.setEnabled(false);
                            button7.setEnabled(false);
                            button8.setEnabled(false);
                            button9.setEnabled(false);
                        }
                    }//end of if statement
            }
            public void actionPerformed(ActionEvent e)
            {

            }
        }

我希望这能帮助任何需要它的人

你提到“底部网格”和按钮,就像我们知道你在说什么一样。请更详细地描述您的问题和程序。假设我们完全不知道你的问题。也考虑使用数组和循环来大大简化你的代码。不要以为我们完全不知道你的问题,相信它。”PeSkelt:我看到2x2板没有问题。从简单开始,然后展开。我确实想知道更多关于他的代码和问题。不确定我是否理解正确,但说真的,你在哪里?