Array/Method/Java help
I have two .java's one called GUI.java, another called CustomPanel.java
Once a button is pressed in GUI.java, it does some stuff:
if (e.getSource() == displayButton)
{
//pageviews1 = Integer.parseInt(myText1.getText());
CustomPanel cp = new CustomPanel();
Graphics g = loginMainPanel.getGraphics();
int pos = 0;
while (pos <= 9)
{
if (values[pos] > biggestvalue)
{
biggestvalue = values[pos];
}
pos = pos + 1;
}
cp.test(g, values[0], values2[0], "1", biggestvalue);
cp.test(g, values[1], values2[1], "2", biggestvalue);
cp.test(g, values[2], values2[2], "3", biggestvalue);
cp.test(g, values[3], values2[3], "4", biggestvalue);
cp.test(g, values[4], values2[4], "5", biggestvalue);
cp.test(g, values[5], values2[5], "6", biggestvalue);
cp.test(g, values[6], values2[6], "7", biggestvalue);
cp.test(g, values[7], values2[7], "8", biggestvalue);
cp.test(g, values[8], values2[8], "9", biggestvalue);
cp.test(g, values[9], values2[9], "10", biggestvalue);
//System.out.println("Added Data: " + values2[count2]);
//count2++;
graphlink.setEnabled(true);
}
You don't really need to know what except cp.test; (It does alot but i've condensed it to fit my problem)
public void test(Graphics g, int pageviews2, String date2, String extcount5, int maxint)
{
...
critxvalues[0] = calc;
crityvalues[0] = desty;
System.out.println(critxvalues[0] + ":" + crityvalues[0]);
}
Then once another button is pressed in GUI.java something else happens:
CustomPanel cp = new CustomPanel();
cp.tooltip(x,y);
This cp.tooltip is simply:
public void tooltip(int x, int y)
{
System.out.println(critxvalues[0] + ":" + crityvalues[0]);
}
Which I hoped would print out the same values as when the array[0] is printed out before, but it doesn't cp.tooltip only prints out 0:0, while cp.test prints out (example) 200:200, so why is the:
critxvalues[0] = calc;
crityvalues[0] = desty;
(in cp.test) not saving the values?
The critxvalue/crityvalue is initalised at the top of CustomPanel.java...
int [] critxvalues = new int[100];
开发者_如何学Goint [] crityvalues = new int[100];
Please help,
Thanks.
Though I'm cannot be sure without seeing all the code, but it seems, that critx|yvalues
are not static, so when you create a new instance of CustomPanel
, they are initialized and contain 0
s. You initialize a CustomPanal
instance in the actionlistener in GUI.java
as well. So the reason is you create new instances of CustomPanel
every time you use it, and this will not save any state unless crit*values
s are not static.
I believe that it happens because these are the values that you assign. You call cp.test() several times. Each time you send there values of arrays value and value2. Unfortunately you did not write where and how do you initiate these arrays.
Check again what do your 2 System.out.println() print. Change format of one of them otherwise you cannot distinguish the output from 2 different places. For better results use debugger.
精彩评论