java - passing multiple JTextFields to a boolean method -


method fourofakind supposed test 4 jtextfields , check if equal each other. each jtextfield excepts 1 number. when press buttonlistener boolean method fourofakind doesn't respond, if returns false, when enter numbers 5555 ex.

import java.awt.*; import java.awt.event.*; import javax.swing.*; /**  */ public class mypanel extends jpanel {     private jlabel inputlabel, outputlabel;     private jbutton button;     private jtextfield digit1, digit2, digit3, digit4;      public mypanel()     {         inputlabel = new jlabel ("enter 4 1 digit numbers between 0 , 9");         button = new jbutton ("result");         outputlabel = new jlabel ("---");          digit1 = new jtextfield(1);         digit1.addactionlistener (new buttonlistener());          digit2 = new jtextfield(1);         digit2.addactionlistener (new buttonlistener());          digit3 = new jtextfield(1);         digit3.addactionlistener (new buttonlistener());          digit4 = new jtextfield(1);         digit4.addactionlistener (new buttonlistener());          add(inputlabel);         add(digit1);         add(digit2);         add(digit3);         add(digit4);         add(button);         add(outputlabel);          setpreferredsize(new dimension(300,100));         setbackground(color.yellow);     }      private class buttonlistener implements actionlistener     {         public void actionperformed(actionevent even)         {             int num1, num2, num3, num4;              string text = digit1.gettext();             num1 = integer.parseint(text);              string text2 = digit2.gettext();             num2 = integer.parseint(text2);              string text3 = digit3.gettext();             num3 = integer.parseint(text3);              string text4 = digit4.gettext();             num4 = integer.parseint(text4);              if (fourofakind(num1, num2, num3, num4))             outputlabel.settext ("four of kind");         }     }      public boolean fourofakind(int a, int b, int c, int d)     {         return (a == b && b == c && c == d);     } } 

the buttonlistener needs added button only, activated when press button , trigger event.

you can remove 'digitn.addactionlistener(new buttonlistener());` , instead use:

button.addactionlistener(new buttonlistener()); 

like that, instructing java listen button being pressed , not text field (which trigger when pressing enter key)


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -