import java.util.*; import javax.swing.border.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; class FlagQuiz { public static void main(String[] args) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); // enable explicit positioning of GUI components contentPane.setLayout( null ); // set up flagJPanel JPanel flagJPanel = new JPanel(); flagJPanel.setBounds( 16, 8, 100, 90 ); flagJPanel.setLayout( null ); flagJPanel.setBorder( new TitledBorder( "Flag" ) ); contentPane.add( flagJPanel ); // set up flagIconJLabel JLabel flagIconJLabel = new JLabel(); flagIconJLabel.setBounds( 10, 14, 80, 80 ); flagIconJLabel.setHorizontalAlignment( JLabel.CENTER ); flagJPanel.add( flagIconJLabel ); // set up selectCountryJLabel JLabel selectCountryJLabel = new JLabel(); selectCountryJLabel.setBounds( 136, 8, 88, 21 ); selectCountryJLabel.setText( "Select country:" ); contentPane.add( selectCountryJLabel ); String[] countries = { "Russia", "China", "United States", "Italy", "Australia", "South Africa", "Brazil", "Spain" }; Arrays.sort( countries ); // sort the array // set up selectCountryJComboBox JComboBox selectCountryJComboBox = new JComboBox( countries ); selectCountryJComboBox.setBounds( 136, 32, 135, 21 ); selectCountryJComboBox.setMaximumRowCount( 3 ); contentPane.add( selectCountryJComboBox ); QuizListener listener = new QuizListener(selectCountryJComboBox, countries, flagIconJLabel); // set up feedbackJTextField JTextField feedbackJTextField = new JTextField(); feedbackJTextField.setBounds( 136, 64, 135, 32 ); feedbackJTextField.setHorizontalAlignment(JTextField.CENTER ); feedbackJTextField.setEditable( false ); contentPane.add( feedbackJTextField ); // set up submitJButton JButton submitJButton = new JButton(); submitJButton.setBounds( 287, 8, 88, 32 ); submitJButton.setText( "Submit" ); contentPane.add( submitJButton ); submitJButton.addActionListener(listener); // set up nextFlagJButton JButton nextFlagJButton = new JButton(); nextFlagJButton.setBounds( 287, 48, 88, 32 ); nextFlagJButton.setText( "Next Flag" ); nextFlagJButton.setEnabled( false ); contentPane.add( nextFlagJButton ); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class QuizListener implements ActionListener { int index; String answer; JComboBox comboBox; String[] countries; JLabel flagIcon; QuizListener(JComboBox comboBox, String[] countries, JLabel flagIcon) { this.comboBox = comboBox; this.countries = countries; this.flagIcon = flagIcon; // create the path for that flag String country = countries[ countries.length -1 - this.index ]; String countryPath = "images/" + country + ".png"; this.answer = country; // set the flagIconJLabel to display the flag flagIcon.setIcon( new ImageIcon( countryPath ) ); } public void actionPerformed(ActionEvent event) { System.out.println("You have guessed: " + countries[this.comboBox.getSelectedIndex()]); System.out.println("The right answer was: " + this.answer); if (this.index < this.countries.length - 1) { this.index += 1; // create the path for that flag String country = countries[ countries.length -1 - this.index ]; String countryPath = "images/" + country + ".png"; this.answer = country; // set the flagIconJLabel to display the flag flagIcon.setIcon( new ImageIcon( countryPath ) ); } else { System.out.println("Sorry, no more flags."); } } }