Java Multidemensional Array Loop that inputs data with Button Action -
Java Multidemensional Array Loop that inputs data with Button Action -
questions pulling hair out over.
how array add together info current index, increment next index add together changed jtextfield info next button press. i'm overwriting same index.i have tried figure out actionlisteners button "submit" uses counter nesting of loop. played ideas question 23331198 , 3010840 , 17829577 many others reference didn't far. searches pull info on managing buttons arrays , creating button arrays assume i'm not using right wording search with. have read few places md array not best alternative use.
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.jtextfield; public class guiandlogictest extends jframe { // variable decloration private static jframe mainframe; private static jpanel mainpanel; private static jtextfield fieldone; private static jtextfield fieldtwo; private static jtextarea textarea; private static jlabel textfieldonelabel; private static jlabel textfieldtwolabel; double[][] tutarray = new double[10][2]; // int =0 ; // set gui method private void gui() { // constructs gui mainframe = new jframe(); mainframe.setsize(800, 800); mainframe.setresizable(false); mainframe.setdefaultcloseoperation(jframe.exit_on_close); mainpanel = new jpanel(); textfieldonelabel = new jlabel("number 1"); mainpanel.add(textfieldonelabel); fieldone = new jtextfield(10); mainpanel.add(fieldone); textfieldtwolabel = new jlabel("number 2"); mainpanel.add(textfieldtwolabel); fieldtwo = new jtextfield(10); mainpanel.add(fieldtwo); textarea = new jtextarea(50, 50); mainpanel.add(textarea); jbutton exit = new jbutton("quit");// quits programme mainpanel.add(exit); exit.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { system.exit(0); } }); jbutton submit = new jbutton("enter"); // reads jfields , set varibles submitted array mainpanel.add(submit); submit.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { double txtfield1 = double .parsedouble(fieldone.gettext().trim()); double txtfield2 = double .parsedouble(fieldtwo.gettext().trim()); if (txtfield1 < 0) { } if (txtfield2 < 0) { } int arrayindex = 0; tutarray[arrayindex][0] = txtfield1; tutarray[arrayindex][1] = txtfield2; arrayindex++; (int = 0; < tutarray.length; i++) { textarea.append("number1: " + tutarray[i][0] + " number2: " + tutarray[i][1]); textarea.append("\n"); textarea.append(string.valueof(arrayindex++)); textarea.append("\n"); // textarea.append(string.valueof(arrayindex++)); } } }); jbutton study = new jbutton(); // used pull info out of array formatting , math mainframe.setvisible(true); mainframe.add(mainpanel); } public static void main(string[] arg) { guiandlogictest guiandlogic = new guiandlogictest(); guiandlogic.start(); } public void start() { gui(); } }
this looking help!
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.jtextfield; public class guiandlogictest extends jframe { // variable decloration private static jframe mainframe; private static jpanel mainpanel; private static jtextfield fieldone; private static jtextfield fieldtwo; private static jtextarea textarea; private static jlabel textfieldonelabel; private static jlabel textfieldtwolabel; double[][] tutarray = new double[10][2]; int arrayindex = 0; // int =0 ; // set gui method private void gui() { // constructs gui mainframe = new jframe(); mainframe.setsize(800, 800); mainframe.setresizable(false); mainframe.setdefaultcloseoperation(jframe.exit_on_close); mainpanel = new jpanel(); textfieldonelabel = new jlabel("number 1"); mainpanel.add(textfieldonelabel); fieldone = new jtextfield(10); mainpanel.add(fieldone); textfieldtwolabel = new jlabel("number 2"); mainpanel.add(textfieldtwolabel); fieldtwo = new jtextfield(10); mainpanel.add(fieldtwo); textarea = new jtextarea(50, 50); mainpanel.add(textarea); jbutton exit = new jbutton("quit");// quits programme mainpanel.add(exit); exit.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { system.exit(0); } }); jbutton submit = new jbutton("enter"); // reads jfields , set varibles submitted array mainpanel.add(submit); submit.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { double txtfield1 = double .parsedouble(fieldone.gettext().trim()); double txtfield2 = double .parsedouble(fieldtwo.gettext().trim()); textarea.settext(""); if (txtfield1 < 0) { } if (txtfield2 < 0) { } tutarray[arrayindex][0] = txtfield1; tutarray[arrayindex][1] = txtfield2; arrayindex++; (int = 0; < arrayindex; i++) { textarea.append("number1: " + tutarray[i][0] + " number2: " + tutarray[i][1]); // textarea.append("\n"); //textarea.append(string.valueof(arrayindex++)); textarea.append("\n"); // textarea.append(string.valueof(arrayindex++)); } } }); jbutton study = new jbutton(); // used pull info out of array formatting , math mainframe.setvisible(true); mainframe.add(mainpanel); } public static void main(string[] arg) { guiandlogictest guiandlogic = new guiandlogictest(); guiandlogic.start(); } public void start() { gui(); } }
you can add together index field in main object:
protected int index;
in actionperformed increment index:
index++;
note:
but why extend jframe , utilize field mainframe?
you can utilize
this.setsize(800,800); this.add(mainpanel);
please declare (because of extends jframe):
private static final long serialversionuid = 1l;
please start name of object lower case letter:
jbutton exit = new jbutton("quit");
java arrays for-loop jbutton
Comments
Post a Comment