Note that there are some explanatory texts on larger screens.

plurals
  1. POLooping Issues in Java
    primarykey
    data
    text
    <p>I'm making a simple currency converter GUI (nothing fancy) as I am going to try and incorporate live exchange rate updates for each time the user opens the application. When creating the layout, I have decided to simple convert 3 currencies (GBP, USD and EUR). I have the respective flags in 2 columns, each column has one of the 3 flags. One column is for the user to select the initial currency and the other is the desired currency to exchange to; as seen below <img src="https://i.stack.imgur.com/0VQRH.jpg" alt="Basic layout with the flags in columns, &#39;From&#39; and &#39;To&#39;"></p> <p>I have created a String array which contains the words "Pounds", "Dollars" and "Euros" and I am wanting to put these labels to the left of the flags (for clarity of the application for the user as not every user may know which currency belongs to which country.</p> <p>I created a loop which would create a label and assign it to the left of the flags, it is supposed to make a "pound" label, then a "Dollar" then a "euro" each time traversing the Y axis south so that they aligns with the flags and it will then reset the array count to go back to the correct string, move along the x-axis and repeat it again. It is however not doing this at all, the only result I get is the text "Pounds " to the left of the first United Kingdom flag; as seen below:</p> <p><img src="https://i.stack.imgur.com/29FhD.jpg" alt="As you can see, i simply get one label"> </p> <p>below is my code if anyone can see as to why this is happening.</p> <p><strong>This is the code which adds the flags to the panel</strong></p> <pre><code> addToMain(GBP1, mainPage, 100,100,100,100); //alligns a United Kingdom Flag to left Column addToMain(GBP2, mainPage, 375,100,100,100); //alligns a United Kingdom Flag to right Column addToMain(USD1, mainPage, 100,200,100,100); //alligns a United States Flag to left Column addToMain(USD2, mainPage, 375,200,100,100); //alligns a United States Flag to right Column addToMain(EUR1, mainPage, 100,300,100,100); //alligns a European Union Flag to left Column addToMain(EUR2, mainPage, 375,300,100,100); //alligns a European Union Flag to right Column </code></pre> <p><strong>This is the loop which should add the text labels to the left of the flags</strong></p> <pre><code> currencyName = new String [] {"Pounds", "Dollars", "Euros"}; for(int i = 0; i &lt;= 7; i++) { int count = 0; //declares a counter for the position in the currencyName array to grab the correct text for label xLabelAlign = 50; yLabelAlign = 100; if(count == 3) { count = 0; //resets to create both columns of labels in the application moves to the next column. xLabelAlign = 325; yLabelAlign = 100; } JLabel temp = new JLabel(currencyName[count]); //creates a new label and names it the string at position count temp.setFont(new Font("SERIF", Font.BOLD, 20)); temp.setForeground(Color.WHITE); addToMain(temp, mainPage, xLabelAlign, yLabelAlign ,100,100); //adds it to the panel yLabelAlign +=100; //moves position ready for the next text label. count++; //grabs the next label in the currencyName string array. } </code></pre> <p><strong>This is the method which adds things to the panel. I have used the set bounds methods to add things to the panel so i can position them where i want easily</strong></p> <pre><code> private void addToMain(JComponent c, JPanel p, int x, int y, int w, int h) { c.setBounds(x,y,w,h); p.add(c); } </code></pre> <p>Any help would be greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload