Java 比较/切换2D阵列中选定的JButton

Java 比较/切换2D阵列中选定的JButton,java,swing,jframe,comparison,jbutton,Java,Swing,Jframe,Comparison,Jbutton,有人问过与我自己类似的问题,但我对如何进行有点不知所措。我确实对java的一些更微妙的细微差别掌握得很差,所以如果有什么不清楚的地方,我很抱歉 比如说,我想比较2D数组中的一个JButton和另一个JButton。更具体地说,所有这些JButton都将存储在二维数组中,并以网格格式显示。所有按钮都具有相同的操作侦听器,该侦听器在按下按钮时调用setselected()方法 如何将这些选定的JButton之一与同一阵列中的另一个选定JButton进行比较?这样做之后,我如何交换位置,或者更具体地说

有人问过与我自己类似的问题,但我对如何进行有点不知所措。我确实对java的一些更微妙的细微差别掌握得很差,所以如果有什么不清楚的地方,我很抱歉

比如说,我想比较2D数组中的一个JButton和另一个JButton。更具体地说,所有这些JButton都将存储在二维数组中,并以网格格式显示。所有按钮都具有相同的操作侦听器,该侦听器在按下按钮时调用setselected()方法

如何将这些选定的JButton之一与同一阵列中的另一个选定JButton进行比较?这样做之后,我如何交换位置,或者更具体地说,交换所述按钮的图标

下面,我已经包含了一些示例代码和我自己对这个主题的尝试。我知道我可以使用.getSource()抓取JButton对象本身,但这不仅允许我一次捕获一个选定的按钮。这都是考虑对每个按钮使用相同的actionlistner代码,但对每个按钮使用一个长期侦听器

下面的代码将每个图标设置为7个随机生成的图像图标中的1个。在主类中生成一个框架。按下或“选择”后,图像图标将更改为同一图像的选定迭代

编辑:根据Ameer的建议,我遇到了几个由actionPerformed方法引起的nullpointer异常。这是因为我的按钮数组此时没有填充按钮对象,还是我只是假设代码中有什么东西

  public class SButtonGame extends JFrame implements ActionListener   {

    public static ImageIcon[] icons={

    new ImageIcon("img1.png"),
    new ImageIcon("img2.png"),
    new ImageIcon("img3.png"),
    new ImageIcon("img4.png"),

    new ImageIcon("img5.png"),

    new ImageIcon("img6.png"),
    new ImageIcon("img7.png"),

    };


    public static ImageIcon[] selectedIcons={

    new ImageIcon("simg1.png"),
    new ImageIcon("simg2.png"),
    new ImageIcon("simg3.png"),
    new ImageIcon("simg4.png"),

    new ImageIcon("simg5.png"),

    new ImageIcon("simg6.png"),
    new ImageIcon("simg7.png"),
    }; 





   int rowNum=0;
   int colNum=0;
   JButton[][] Buttons; 

   boolean swaptf=false;
   JButton CButton; // Selected button "holder". Doesn't accomplish anything I think it            should






    public SButtonGame(String title) {

    //Constructs frame
    super(title);
    getContentPane().setLayout(null)
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(578,634);









    int colLoc=10;
    int rowLoc=10;  
    this.colNum=0;
    this.rowNum=0;


    for(int r=0; r<8; r++)
    {

    this.Buttons= new JButton[9][9];

    this.rowNum++;

    for(int c=0; c<8; c++)
    {

    ActionListener listner = new ActionListener(){

    public void actionPerformed(ActionEvent e)
    {

    if(e.getSource() instanceof JButton)
    {




    ((JButton) e.getSource()).setSelected(true);

    CButton=(JButton)e.getSource();





    }

    }    


    };







    int ranImg;

    ranImg=0+(int)(Math.random()*7);
    int sranImg=ranImg;


    this.Buttons[this.colNum][this.rowNum]= new JButton(icons[ranImg]);
    this.Buttons[this.colNum][this.rowNum].setSelectedIcon(selectedIcons[sranImg]);
    this.Buttons[this.colNum][this.rowNum].addActionListener(listner);
    this.Buttons[this.colNum][this.rowNum].setSize(59,59);
    this.Buttons[this.colNum][this.rowNum].setLocation(rowLoc,colLoc);
    rowLoc=rowLoc+69;

    this.Buttons[this.colNum][this.rowNum].setVisible(true);

    this.Buttons[this.colNum]  [this.rowNum].setBorder(BorderFactory.createLineBorder(Color.black));
    add(this.Buttons[this.colNum][this.rowNum]);


    }
    this.colNum++;
    colLoc=colLoc+69;    
    rowLoc=10;
    } 




    JButton Newgame;
    Newgame= new JButton("NewGame");
    Newgame.setSize(100, 30);
    Newgame.setLocation(350, 560);
    Newgame.setVisible(true);
    add(Newgame);



    JButton Quit;
    Quit= new JButton("Quit");
    Quit.setSize(60, 30);
    Quit.setLocation(480, 560);
    Quit.setVisible(true);
    add(Quit);





    New.addActionListener(new ActionListener() 
    {
    //dispose of current frame and generates a new one;

    public void actionPerformed(ActionEvent e)
    {

    dispose();
    SButtonGame Frame;
    Frame = new SButtonGame("ShinyButtons");

    Frame.setVisible(true);  

    }


    });
    Quit.addActionListener(new ActionListener(){


    public void actionPerformed(ActionEvent e)

    {


    dispose();


    }





    });

    }


 @Override
            public void actionPerformed(ActionEvent ae){

                if(ae.getSource() instanceof JButton){

                    JButton sButton;
                   int rindex=0;
                   int cindex=0;

                    ((JButton) ae.getSource()).setSelected(true);

                   sButton=(JButton)ae.getSource();




                   if(SButtonGame.this.Buttons[(int)sButton.getClientProperty("rownum")][(int)sButton.getClientProperty("colnum")].isEnabled()==true){





                   }



                }



            }    







    public static void main(String[] args) 
    {

    SButtonGame Frame;
    Frame = new SButtonGame("ButtonsGame");

    Frame.setVisible(true);




    }

    }
公共类SButtonGame扩展JFrame实现ActionListener{
公共静态图像图标[]图标={
新图像图标(“img1.png”),
新图像图标(“img2.png”),
新图像图标(“img3.png”),
新图像图标(“img4.png”),
新图像图标(“img5.png”),
新图像图标(“img6.png”),
新图像图标(“img7.png”),
};
公共静态图像图标[]选择指令={
新图像图标(“simg1.png”),
新图像图标(“simg2.png”),
新图像图标(“simg3.png”),
新图像图标(“simg4.png”),
新图像图标(“simg5.png”),
新图像图标(“simg6.png”),
新图像图标(“simg7.png”),
}; 
int rowNum=0;
int colNum=0;
JButton[]]按钮;
布尔值=false;
JButton CButton;//选中的按钮“holder”。没有完成我认为应该完成的任何任务
公共SButtonGame(字符串标题){
//构造框架
超级(标题);
getContentPane().setLayout(空)
setDefaultCloseOperation(关闭时退出);
设置大小(578634);
int colLoc=10;
int rowLoc=10;
这个.colNum=0;
这个.rowNum=0;
对于(int r=0;r内部actionPerformed(ActionEvent e)方法,您可以使用SButtonGame访问按钮的2D数组。此.buttongame(理想情况下,变量名称应该是以小b开头的按钮)


然后,您可以将单击的按钮与数组中的按钮进行比较,并完成其余工作。

请正确设置代码格式。