Question: Create an application that displays some flags for different countries. Each flag will be matched with a button. When a button is pressed the country
Create an application that displays some flags for different countries. Each flag will be matched with a button. When a button is pressed the country name will be displayed (or some other interesting information).
- Create a new project.
- Create a folder in the root of the project called images.
- Save the attached flags icons in the images folder.
- Create a new Flags class
- The class must extend JFrame and implement ActionListener.
- In the Flags constructor, set up a GridLayout that is three rows tall and three columns wide. I set up gaps of five horizontal and vertical.
- Set the layout for the Flags class to the GridLayout in step 2a.
- Create the images and buttons for each flag
- Create a Panel for each flag and button combination.
- Set up a new GridLayout for each button / image combination that is two rows tall and one column wide. I set up gaps of three horizontal and vertical.
- Set the Layout of the flag and button panel.
- JLabels allow text labels or icon labels. Put a flag icon in a JLabel by creating a new ImageIcon with one of the GIF files attached. The name will be the icon name prefixed with the images directory name. For example: JLabel flag = new JLabel(new ImageIcon("images/China.gif")).
- Create a button with the country name for the button.
- Add an actionListener on the button that is the Flags object.
- Add the flag icon JLabel and the button to the Flag layout.
- Set a tooltip on the flag and button panel that says "Flag of " and the country name.
- Add the panel to the Flag class.
- Repeat for each country. This is a good opportunity for a method to remove duplication.
- Create the actionPerformed() method to implement the ActionListener.
- Get the source of the action, which will be a JButton. It will be necessary to cast the Object to a JButton.
- Create a JOptionPane to display "Button pressed " and the country name by displaying the button text.
- In the main method, instantiate the Flag class.
- Set the title.
- Set the windows size. 700 by 700 worked fine.
- Set the default close operation to EXIT_ON_CLOSE, so the application will end cleanly.
- Finally, set visible to true.
- Run the application and test the presentation and functionality.
- As required for all assignments from now on
- validate the proper functionality using the example answers below and review the Concepts and Ideas checklist format the code
- add a comment at the top of the file that reads @ author and your name
- build the javadocs
- Verify that the jar file contains source and class files and the MANIFEST.MF file contains a Main-Class. See the Resources section for Configuring Eclipse to view Jar files.
- Run the application from the jar file, either from the command line using java -jar jarfilename or from Eclipse. See the Resources section for Configuring Eclipse to run Jar files.
- submit the jar file in the Assignment section of BlackBoard under View/Complete Assignment for this assignment.
Example:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
