I have a problem how to transfer my European CApitals Array into JLabel Array, without writing the capitals on each JLabel. I figured out my dropdown menues, but I want to display the counties on JLabels and parallel to it, the textboxes for the capitals, with the JButon next to it to check the answer. I created another array for the countries to avoid the empty string when creating the JLabel. I have been trying to figure out this for a week now. Here is my code:
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class JEurope extends JFrame implements ActionListener
{
String[] countryStrings = {"", "Albania", "Andorra", "Armenia", "Austria", "Azerbaijan",
"Belarus", "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Cyprus",
"Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany",
"Greece", "Hungary", "Iceland", "Ireland", "Italy", "Kazakhstan", "Latvia",
"Liechtenstein", "Lithuania", "Luxembourg", "Republic of Macedonia", "Malta",
"Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal",
"Romania", "Russia", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain",
"Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City"};
String[] countryStrings1 = {"Albania", "Andorra", "Armenia", "Austria", "Azerbaijan",
"Belarus", "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Cyprus",
"Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany",
"Greece", "Hungary", "Iceland", "Ireland", "Italy", "Kazakhstan", "Latvia",
"Liechtenstein", "Lithuania", "Luxembourg", "Republic of Macedonia", "Malta",
"Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal",
"Romania", "Russia", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain",
"Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City"};
String[] capitalStrings = {"", "Tirana", "Andorra", "Yerevan", "Vienna", "Baku", "Minsk",
"Brussels", "Sarajevo", "Sofia", "Zagreb", "Nicosia", "Prague", "Copenhagen", "Tallinn",
"Helsinki", "Paris", "Tbilisi", "Berlin", "Athens", "Budapest", "Reykjavík", "Dublin",
"Rome", "Astana", "Riga", "Vaduz", "Vilnius", "Luxembourg", "Skopje", "Valletta",
"Chisinau", "Monaco", "Podgorica", "Amsterdam", "Oslo", "Warsaw", "Lisbon", "Bucharest",
"Moscow", "San Marino", "Belgrade", "Bratislava", "Ljubljana", "Madrid", "Stockholm",
"Bern", "Ankara", "Kiev", "London", "Vatican City" };
String[] euStrings = {"", "no", "no", "no", "yes", "no",
"no", "yes", "no", "yes", "no", "yes",
"yes", "yes", "yes", "yes", "yes", "no", "yes",
"yes", "yes", "no", "yes", "yes", "no", "yes",
"no", "yes", "yes", "no", "yes",
"no", "no", "no", "yes", "no", "yes", "yes",
"yes", "no", "no", "no", "yes", "yes", "yes",
"yes", "no", "no", "no", "yes", "no"};
Container con;
JLabel mainLabel = new JLabel("Check your knowledge about Europe Capitals");
JLabel countryLabel = new JLabel("Select a country!");
JComboBox countries = new JComboBox(countryStrings);
JLabel capitalLabel = new JLabel("Match the Capital!");
JComboBox capitals = new JComboBox(capitalStrings);
String answerString = " ";
JLabel mainLabel1 = new JLabel("Is this state a member of EU");
JComboBox countries1 = new JComboBox(countryStrings);
JButton answerButton1 = new JButton("Check your answer");
String answerString1 = " ";
int x;
ImageIcon europe = new ImageIcon("capitals11.jpg");
JLabel mapEurope = new JLabel(europe);
JLabel[] arrayLabel = new JLabel[countryStrings1.length];
ImageIcon eu = new ImageIcon("eu_flag.jpg");
JLabel flag = new JLabel(eu);
JButton answerButton = new JButton("Check your answer");
Font font1 = new Font("Arial", Font.BOLD, 26);
Font font2 = new Font("Arial", Font.BOLD, 18);
public JEurope()
{
super("European Capitals");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = getContentPane();
MigLayout layout = new MigLayout ("wrap 2", "[]40[]", "[][]20[]");
mainLabel.setFont(font1);
mainLabel1.setFont(font1);
countryLabel.setFont(font2);
capitalLabel.setFont(font2);
answerButton.setFont(font2);
answerButton1.setFont(font2);
capitals.setFont(font2);
countries.setFont(font2);
countries1.setFont(font2);
JPanel pane = new JPanel(layout);
con.add(pane);
pane.add(mapEurope, "width 100%");
pane.add(flag, "width 100%");
pane.add(mainLabel, "span 2, align center");
pane.add(countryLabel, "width 50%, align center");
pane.add(capitalLabel, "width 50%, align center, wrap");
pane.add(countries, "width 50%, align center");
pane.add(capitals, "width 50%, align center, wrap");
pane.add(answerButton, "span, align center" );
answerButton.addActionListener(this);
pane.add(mainLabel1, "span 2, align center");
pane.add(countries1, "width 50%, align center");
pane.add(answerButton1, "align center" );
answerButton1.addActionListener(this);
pane.setBackground(Color.YELLOW);
for(x = 0; x <countryStrings1.length; ++x)
arrayLabel[1] = new JLabel("Albania");
arrayLabel[2] = new JLabel("Andorra");
arrayLabel[3] = new JLabel("Armenia");
pane.add(arrayLabel[1]);
pane.add(arrayLabel[2]);
pane.add(arrayLabel[3]);
}
public static void main(String[] args)
{
final int WIDTH = 700;
final int HEIGHT = 600;
JFrame.setDefaultLookAndFeelDecorated(true);
JEurope frame = new JEurope();
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int countriesIndex = countries.getSelectedIndex();
int capitalsIndex = capitals.getSelectedIndex();
int countriesIndex1 = countries1.getSelectedIndex();
Object pipi = countries1.getSelectedItem();
Object pipiCountries = countries.getSelectedItem();
Object pipiCapitals = capitals.getSelectedItem();
if (euStrings[countriesIndex1] == "yes")
{
answerString1 = ( pipi + " is a member of EU. ");
}
else
{
answerString1 = ( pipi + " is not a member of EU. ");
}
mainLabel1.setText(answerString1);
//int euIndex = how do I compare this index w third yes/no array
if (countriesIndex == capitalsIndex) //if I want to cite the right answer - capital and country
{
answerString = "Congratulations! " + pipiCapitals + " is the capital of " + pipiCountries + ".";
//answerString = "Congratulations! " + capitals.getText?? ;
}
else
answerString = "Try again! " + pipiCapitals + " is not the capital of " + pipiCountries + ".";
mainLabel.setText(answerString);
}
}