java - groovy ActionListener doesn't work under one condition -
I am writing simple calc in groovy with swing, I declare a button in a loop and at the same time, Add Listener. It seems that listeners work for each button except the "=" button, I do not know what's the problem here. Can you help me with that?
import javax.swing. Import java.awt. Import java.awt.event. Frame = new jefram (size: [600, 300], layout: new flow layout ()) MsgLabel = new JLab ("") frame.contentPane.add msgLabel def character = "0123456789, / * - + = C" 0. Upto (characters.length () - 1) {df signal = characters.getit (this) button = new pocket (sign) frame. ContentPane.add button println this.getClass (). GetName () if {sign == "="} {button.addActionListener {{equals ()}} as ActionListener and if {sign = = "C"} {button.addActionListener (Action_istener) {{explicitly ()}} {Button. AddActionListener ({putText (sign)}}}}}}}}}}}}}}}}}}}}}}}}} equal to public zero equal to () {println "equals"} public zero clear () {println "clear"} Public Zero PutText (sign) {println sign}
Thanks in advance!
Change the "equal" method to "result" and you have done it:
button.addActionListener (as on ActionListener { You are probably suffering to avoid the problem of Hansing / scope: / P>
button.addActionListener (as the ActionListener {this.equals ()} [...] public zero equivalent (println "is equal to")
< P> I'm not sure which object is hiding the par ()
method, but I have just tested the above solutions in the Georiz console (2.3) .6) And they work Are there. Edit : You can avoid the pressure of shutting down to button.actionPerformed = close
, which means: <(Sign == '=') {button.actionPerformed = {println '='}} and if (sign == 'c') {button.actionPerformed = {println 'clear}} other {button.actionPerformed = {println sign} }
Comments
Post a Comment