2x2国际象棋棋盘

时间:2013-11-06 16:14:16

标签: 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 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);
                }

            }
        }

4 个答案:

答案 0 :(得分:0)

添加到actionPerformed功能:在每个动作检查当前位置之前,如果按f / e时它是左下角。按钮2什么都不应该发生。 或者我没有得到你的问题。

答案 1 :(得分:0)

你记下典当的位置

 int x=0, y=0;

当您移动pawn时,检查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
 }

答案 2 :(得分:0)

  

我禁止某些网格的某些按钮但不相同   所有网格的按钮

GridLayout使用Component的left-to-right方向,它会根据您添加的顺序布置组件。所以对于2x2 gird:

 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) 

因此,要在网格单元(row, col)中找到一个组件并使用它,它实际上很简单: 首先,找到组件的索引,然后让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);

答案 3 :(得分:0)

好吧,我知道我有点迟到了,我可能应该比现在早一点回来但是我想分享我的代码,以防万一有其他想要制作的机构国际象棋棋盘我只做了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)
            {

            }
        }

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