In: Computer Science
Write a class GBP2HKD containing the following methods:
(a) constructor : which builds the frame shown on the right side. The frame consists of
• a pull-down menu "Action" with menu items "Convert" and "Exit",
• a textfield to enter GBP amount. Some text is in the text field to provide information to the user on what to input. The user needs to delete the text before entering the numbers.
• a label to display the amount of HKD (please use the exchange rate of 1 GBP to exchange for 9.9 HKD).
Copy the class, including import statement(s), as the answers to this part.
(b) actionPerformed() : which perform necessary actions when each menu item is selected. When "Convert" is selected, the amount of HKD is calculated and displayed on the label. When "Exit" is selected, the program terminates.
(c) main() : which creates an GBP2HKD object and sets it visible for testing.
GBP2HKD.java:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GBP2HKD extends JFrame implements
ActionListener {
private static JMenuItem
convert_Menu, exit_Menu;
private static JTextField
inp_Field;
private static JLabel
res_Label;
private static final double
GBP_TO_HKD = 9.9;
public GBP2HKD()
{
// initialize
the main frame and the main panel
JPanel panel =
new JPanel(new FlowLayout(FlowLayout.CENTER));
// initialize
the menu bar, menu and menu items
JMenuBar
menu_Bar = new JMenuBar();
JMenu menu = new
JMenu("Action");
convert_Menu =
new JMenuItem("Convert");
exit_Menu = new
JMenuItem("Exit");
menu.add(convert_Menu);
menu.add(exit_Menu);
menu_Bar.add(menu);
// initialize
the panel and the textfield and the result label to the panel
JPanel panel_1 =
new JPanel(new GridLayout(2, 0));
inp_Field = new
JTextField(20);
inp_Field.setText("GBP Amount");
res_Label = new
JLabel("Result:");
panel_1.add(inp_Field);
panel_1.add(res_Label);
// add the sub
panel to the main panel
panel.add(panel_1);
// finally, add
the main panel to the JFrame
setJMenuBar(menu_Bar);
add(panel);
// action
listener for menu items
convert_Menu.addActionListener(this);
exit_Menu.addActionListener(this);
}
@Override
public void
actionPerformed(ActionEvent ae)
{
//ae = action
event
if(ae.getSource() == convert_Menu)
{
// convert the amount
if(inp_Field.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please enter a GBP
amount");
return;
}
double amnt =
Double.parseDouble(inp_Field.getText().trim());
double res = convertAmount(amnt);
res_Label.setText(String.format("$%.2f",
res));
}
else
if(ae.getSource() == exit_Menu)
System.exit(0);
}
// method to convert GBP amount to
HKD amount
private static double
convertAmount(double amnt)
{
return(amnt *
GBP_TO_HKD);
}
}
GBP_Test.java:
import javax.swing.JFrame;
public class GBP_Test {
public static void main(String[]
args) {
GBP2HKD
gpb_Object = new GBP2HKD();
gpb_Object.setTitle("GBP To HKD");
gpb_Object.setSize(250, 120);
gpb_Object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gpb_Object.setLocationRelativeTo(null);
gpb_Object.setVisible(true);
}
}
Output Screenshot: