arrays - Looping Issues in Java -
i'm making simple currency converter gui (nothing fancy) going try , incorporate live exchange rate updates each time user opens application. when creating layout, have decided simple convert 3 currencies (gbp, usd , eur). have respective flags in 2 columns, each column has 1 of 3 flags. 1 column user select initial currency , other desired currency exchange to; seen below
i have created string array contains words "pounds", "dollars" , "euros" , wanting put these labels left of flags (for clarity of application user not every user may know currency belongs country.
i created loop create label , assign left of flags, supposed make "pound" label, "dollar" "euro" each time traversing y axis south aligns flags , reset array count go correct string, move along x-axis , repeat again. not doing @ all, result text "pounds " left of first united kingdom flag; seen below:
below code if can see why happening.
this code adds flags panel
addtomain(gbp1, mainpage, 100,100,100,100); //alligns united kingdom flag left column addtomain(gbp2, mainpage, 375,100,100,100); //alligns united kingdom flag right column addtomain(usd1, mainpage, 100,200,100,100); //alligns united states flag left column addtomain(usd2, mainpage, 375,200,100,100); //alligns united states flag right column addtomain(eur1, mainpage, 100,300,100,100); //alligns european union flag left column addtomain(eur2, mainpage, 375,300,100,100); //alligns european union flag right column
this loop should add text labels left of flags
currencyname = new string [] {"pounds", "dollars", "euros"}; for(int = 0; <= 7; i++) { int count = 0; //declares counter position in currencyname array grab correct text label xlabelalign = 50; ylabelalign = 100; if(count == 3) { count = 0; //resets create both columns of labels in application moves next column. xlabelalign = 325; ylabelalign = 100; } jlabel temp = new jlabel(currencyname[count]); //creates new label , names string @ position count temp.setfont(new font("serif", font.bold, 20)); temp.setforeground(color.white); addtomain(temp, mainpage, xlabelalign, ylabelalign ,100,100); //adds panel ylabelalign +=100; //moves position ready next text label. count++; //grabs next label in currencyname string array. }
this method adds things panel. have used set bounds methods add things panel can position them want easily
private void addtomain(jcomponent c, jpanel p, int x, int y, int w, int h) { c.setbounds(x,y,w,h); p.add(c); }
any appreciated.
fast solution: move int count = 0; xlabelalign = 50; ylabelalign = 100;
out of for
loop. loop in range of [0,5].
good solution: java layouts tutorial
Comments
Post a Comment