简体   繁体   English

Java ActionListener(多个语句)

[英]Java ActionListener (multiple statements)

The following is the edited and (extremely basic version) of my codes so you could understand clearly what it is that i am trying to do. 以下是我的代码的编辑和(极其基本版本),因此您可以清楚地了解我正在尝试做什么。 I would like to use: 我想用:

public void actionPerformed(ActionEvent e){ }    

The problem is that i can't edit/ manupulate moves for my chess pieces in the loops under ActionListener but it never functions all statement, it always only functions on one if statement and ignores the others. 问题是我无法在ActionListener下的循环中编辑/操作棋子的动作,但它从不运行所有语句,它始终只在一个if语句上运行而忽略其他语句。

if(e.getSource()==cSquare[a][b]){  //current source
    if(switcher=="1" && cSquare[a][b].getChessPiece()=="Pawn" ) {}

Simple example of ActionListener: ActionListener的简单示例:

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

.... ....

 elements[i][x] = new OriginalElements(i, x, "None");
                elemenents[i][j].setIcon(None);   

....... .......

        if(elements[i][x].Xpos()==2 && elements[i][x].Ypos()==2){
                        elements[i][x].setIcon(Image);
                        cSquare[i][x].setChessPiece("Image");
                    }
                    elements[i][j].addActionListener(cListener);   //Click do this
                    panel.add(elements[i][jx);  

My question would be; 我的问题是; how could i implement multiple (if, else etc) statements into the ActionListener and make it work on all? 我怎样才能在ActionListener中实现多个(if,else等)语句并使其适用于所有语句? rather than only one. 而不是只有一个。 I've tried every possibility i know but only seems to work on a single element (eg move image from square to another square and replace current square image with 'EmptyImage'. 我已经尝试了所有我认识的可能性,但似乎只对单个元素起作用(例如将图像从正方形移动到另一个正方形并用'EmptyImage'替换当前正方形图像。
Thank you for your time. 感谢您的时间。

PS : ignore the "professonality" of the codes. PS:忽略代码的“专业性”。 I changed it to give you a clear understanding;; 我改变了它,让你清楚了解;; thanks :) 谢谢 :)

You have various errors in your code: 您的代码中有各种错误:

if(e.getSource()==cSquare[a][b]){  //current source
    if(switcher=="1" && cSquare[a][b].getChessPiece()=="Pawn" ) {}

Strings are compared with the .equals(String str) method. 将字符串与.equals(String str)方法进行比较。

 if(elements[i][x].Xpos()==2 && elements[i][x].Ypos()==2){
                        elements[i][x].setIcon(Image);
                        cSquare[i][x].setChessPiece("Image");

If statements do not end with a semi colon ( ; ). 如果语句不以半冒号( ; )结尾。

Fix those and see how it goes. 解决这些问题,看看它是怎么回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM