Passing variables to another classes (Java) -
i have java program contains multiple classes, of extend jpanel (except class containing main method). trying pass int variables 1 class and, using asked questions, came this:
class rollpanel
public int getbraincount() { return braincount; }
class brainspanel (where trying send variable)
void setbraincount() { rollpanel rollpanel = new rollpanel(); braincount = rollpanel.getbraincount(); }
this doesn't give errors, when add braincount label see value, 0.
braincount declared @ class level of rollpanel, given value code in button listener:
value1 = generator.nextint(3) + 1; value2 = generator.nextint(3) + 1; value3 = generator.nextint(3) + 1; //rolltotal++; //counts how many brains rolled. if (value1 == 1) braincount++; if (value2 == 1) braincount++; if (value3 == 1) braincount++;
i understand declaring variable automatically mean value 0 @ first(which assume why displayed zero), how can pass updated value after above code brainspanel can add value label in brainspanel? getbraincount() come after button listener? feel i'm overlooking simple here...
edit: think problem isn't setter getter. since in rollpanel(where getter is) declare braincount int initial value 0, when getter gets value remains @ 0 though braincount should altered in button listener. here rollpanel in entirety. how can make getter updated value of braincount instead of initial 0?
package zombiedice; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class rollpanel extends jpanel { jlabel die1label, die2label, die3label, testlabel; jbutton rollbutton, sortbutton; jpanel dicepanel, buttonpanel; imageicon die1, die2, die3; int rolltotal, value1, value2, value3; int braincount, blastcount; random generator = new random(); public rollpanel() { setlayout(new boxlayout(this, boxlayout.y_axis)); setbackground(color.black); dicepanel = new jpanel(); dicepanel.setbackground(color.black); //creates blank imageicons used later display dice. die1 = new imageicon(); die2 = new imageicon(); die3 = new imageicon(); //a panel hold buttons. buttonpanel = new jpanel(); //creates , links roll button rolllistener. rollbutton = new jbutton("roll"); rollbutton.addactionlistener(new rolllistener()); buttonpanel.add(rollbutton); //after roll, button need clicked brain , blast //die can sorted proper catergories. sortbutton = new jbutton("sort"); sortbutton.addactionlistener(new sortlistener()); //creates labels out of dice images. die1label = new jlabel(die1); die2label = new jlabel(die2); die3label = new jlabel(die3); //adds image labels panel holds dice. dicepanel.add(die1label); dicepanel.add(die2label); dicepanel.add(die3label); add(dicepanel); add(buttonpanel); } //closes constructor //roll button listener. private class rolllistener implements actionlistener { public void actionperformed(actionevent event) { rollbutton.setenabled(false); repaint(); buttonpanel.add(sortbutton); repaint(); sortbutton.setenabled(true); repaint(); value1 = generator.nextint(3) + 1; value2 = generator.nextint(3) + 1; value3 = generator.nextint(3) + 1; //rolltotal++; //counts how many brains rolled. if (value1 == 1) braincount++; if (value2 == 1) braincount++; if (value3 == 1) braincount++; //updates dice die1label.seticon(new imageicon(value1 + ".png")); die2label.seticon(new imageicon(value2 + ".png")); die3label.seticon(new imageicon(value3 + ".png")); } //closes actionperformed } //closes listener roll button //sort button listener. private class sortlistener implements actionlistener { public void actionperformed(actionevent event) { sortbutton.setenabled(false); repaint(); rollbutton.setenabled(true); repaint(); } //closes actionperformed. }//closes sort button listener. public int getbraincount() { return braincount; } } //closes class
brainspanel
package zombiedice; import javax.swing.*; import java.awt.*; public class brainspanel extends jpanel { imageicon icon1 = new imageicon(); imageicon icon2 = new imageicon(); jlabel braintotal, label1, label2; jpanel brainpanel; int braincount; void setbraincount(int count) { // rollpanel rollpanel = new rollpanel(); braincount = count; //braincount = count; } public brainspanel() { setlayout(new boxlayout(this, boxlayout.y_axis)); setbackground(color.black); icon1 = new imageicon("1.png"); icon2 = new imageicon("1.png"); label1 = new jlabel(icon1); label2 = new jlabel(icon2); braintotal = new jlabel ("brains eaten: " + braincount); add(label1); add(label2); add(braintotal); } //closes constructor } //closes class
in peace of code:
void setbraincount() { rollpanel rollpanel = new rollpanel(); braincount = rollpanel.getbraincount(); }
you create new rollpanel
object before getting braincount
. of course 0 if declared field in rollpanel
. variable newly initialized object creation line before.
you should make sure use same rollpanel
object, e.g. keep field in brainspanel
. it's hard sure given code.
Comments
Post a Comment