【问题标题】:Comparing/Switching Selected JButtons within a 2D Array比较/切换 2D 数组中选定的 JButton
【发布时间】:2014-03-04 02:45:27
【问题描述】:

有人问过我自己的类似问题,但我不知道如何继续。我真的对 java 的一些更微妙的细微差别不太了解,所以如果有任何不清楚的地方,我深表歉意。

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

我将如何比较这些选定的 JButton 与同一数组中的另一个选定的 JButton?这样做后,我怎么能交换位置,或者更具体地说,交换所述按钮的图标。

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

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

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

  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);




    }

    }

【问题讨论】:

  • 请正确格式化您的代码。

标签: java swing jframe comparison jbutton


【解决方案1】:

在 actionPerformed(ActionEvent e) 方法中,您可以使用 SButtonGame.this.Buttons 访问按钮的二维数组(理想情况下,变量名称应该是以小 b 开头的按钮)。

然后,您可以将单击的按钮与数组中的按钮进行比较,然后执行其他操作。

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2013-04-20
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2012-10-16
    • 2013-09-17
    相关资源
    最近更新 更多