java - How to set variables from multiple text fields in single jPanel -
brand new coder here. i've been searching around, cannot seem find topics on how set multiple variables line of textboxes in jpanel use later algorithmic functions. in case, need 5 unique variables later use. setting these variables highly appreciated. here's code have setting text fields , gathering user input:
import java.util.scanner; import javax.swing.*; //used create jpanel public class simplemath { public static void main(string[] args) { //setup text boxes jtextfield afield = new jtextfield(5); jtextfield bfield = new jtextfield(5); jtextfield cfield = new jtextfield(5); jtextfield dfield = new jtextfield(5); jtextfield efield = new jtextfield(5); //creating jpanel jpanel mypanel = new jpanel(); mypanel.add(new jlabel("1:")); mypanel.add(afield); mypanel.add(box.createhorizontalstrut(15)); //a spacer mypanel.add(new jlabel("2:")); mypanel.add(bfield); mypanel.add(box.createhorizontalstrut(15)); mypanel.add(new jlabel("3:")); mypanel.add(cfield); mypanel.add(box.createhorizontalstrut(15)); mypanel.add(new jlabel("4:")); mypanel.add(dfield); mypanel.add(box.createhorizontalstrut(15)); mypanel.add(new jlabel("5:")); mypanel.add(efield); //gathering data int result = joptionpane.showconfirmdialog(null, mypanel, "please enter 5 integers", joptionpane.ok_cancel_option); if (result == joptionpane.ok_option) { system.out.println("value 1: " + afield.gettext()); system.out.println("value 2: " + bfield.gettext()); system.out.println("value 3: " + cfield.gettext()); system.out.println("value 4: " + dfield.gettext()); system.out.println("value 5: " + efield.gettext()); scanner input = new scanner(system.in); } } }
you're putting static main method, , trying create organic viable , complex java program, , won't work. need stop you're doing , first learn java basics including how create , use instance fields , non-static methods. it's these fields available mutation in other parts of program if created correctly. decent book or tutorial , start learning first principles before doing gui programming -- won't regret doing this.
Comments
Post a Comment