class - How do I use the outcome of my actionPerformed method to effect the main method - java -
i have class called screen containing actionperformed method. want different outcome different menu items: random, aggressive , human.
this outcome effects main method, unsure how link two...
public class screen extends jframe implements actionlistener { actionlistener listener; jmenuitem random = new jmenuitem("random"); jmenuitem aggressive = new jmenuitem("aggressive"); jmenuitem human = new jmenuitem("human"); public screen(board board){ //menubar items menu.add(random); random.addactionlistener(this); menu.add(aggressive); aggressive.addactionlistener(this); menu.add(human); human.addactionlistener(this); .... //sets board of buttons , adds actionlistener each. .... } public void actionperformed(actionevent e) { if(e.getsource() == random){ } if(e.getsource() == aggressive){ } if(e.getsource() == human){ } //code board buttons - nothing menu. //but thought might if (numclicks == 0){ jbutton piece = (jbutton) e.getsource(); string xy = piece.getname(); string x = xy.substring(0,1); string y = xy.substring(2,3); fromxint = integer.parseint(x); fromyint = integer.parseint(y); system.out.println("from" + " " +fromxint + "," + fromyint); } else{ jbutton piece = (jbutton) e.getsource(); string xy = piece.getname(); string x = xy.substring(0,1); string y = xy.substring(2,3); toxint = integer.parseint(x); toyint = integer.parseint(y); system.out.println("to" + " " + toxint + "," + toyint); } numclicks++; if (numclicks >= 2){ numclicks = 0; } return; } }
my class contains main method:
public class chess{ public static void main(string [ ] args){ screen s = new screen(board); // attempt doesn't work if (s.actionperformed(e) == random){ .....
note: new java , still trying head round linking of multiple classes. --------------------the actionperformed method contains events if buttons clicked haven't added code in because on complicates things.--
this approach uses public enum , sets style variable according users menu choice:
package chess; //... public class screen extends jframe implements actionlistener { private jmenuitem random = new jmenuitem("random"); private jmenuitem aggressive = new jmenuitem("aggressive"); private jmenuitem human = new jmenuitem("human"); public enum playstyle {random, aggressive, human}; private playstyle style; public screen(board board) { //menubar items menu.add(random); random.addactionlistener(this); menu.add(aggressive); aggressive.addactionlistener(this); menu.add(human); human.addactionlistener(this); //.... //sets board of buttons , adds actionlistener each. } @override public void actionperformed(actionevent e) { if (e.getsource() == random) { style=playstyle.random; } if (e.getsource() == aggressive) { style=playstyle.aggressive; } if (e.getsource() == human) { style=playstyle.human; } //code board buttons - nothing menu. //.... } public playstyle getstyle(){ return style; } }
this class contains main method:
package chess; import chess.screen.playstyle; public class chess{ public static void main(string [ ] args){ screen s = new screen(board); // attempt work if (s.getstyle()==playstyle.random) { ... } else if (s.getstyle()==playstyle.aggressive){ ...
Comments
Post a Comment