Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add code to the buttonShowNext that will do this following when the user clicks:

ID: 668512 • Letter: A

Question

Add code to the buttonShowNext that will do this following when the user clicks:

Create a Try and Catch block statement to store the code and trap the error if one occurs.

Declare a string variable called Currency

Declare a double variable called Amount

Then set the value of the Amount variable to the text value in the textBoxAmount

Then set the value of the Currency variable to the value in the textBoxCurrency

You will need to Parse the string stored in the text box (textBoxAmount) to a double in order to successfully assign its value to the double Amount variable.

Display a string in a Message Box similar to the following:

  You have selected to convert "Amount" "Currency"

Finally, if the user enters bad data (non-numeric in the txtBoxAmount text box), a Message box pops up and says:

  An error has occurred while parsing the amount entered!

Explanation / Answer

import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.InputVerifier; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class ConverterFrame extends JFrame { private static final long serialVersionUID = 9163427800892086619L; private JLabel amountLabel; private JTextField amountTextBox; private JLabel fromCurrencyLabel; private JComboBox fromCurrencyCombobox; private JLabel toCurrencyLabel; private JComboBox toCurrencyCombobox; private JButton convertButton; private JLabel resultLabel; private ConverterLogic converter; public ConverterFrame(ConverterLogic converter) { super(); this.converter = converter; initializeComponents(); initializeListeners(); } private void initializeListeners() { convertButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { double amount = Double.parseDouble(amountTextBox.getText()); Currency from = (Currency) fromCurrencyCombobox .getSelectedItem(); Currency to = (Currency) toCurrencyCombobox.getSelectedItem(); double result = converter.convert(amount, from, to); resultLabel.setText(String.format("%.2f", result)); } }); amountTextBox.setInputVerifier(new InputVerifier() { @Override public boolean verify(JComponent arg0) { JTextField textField = (JTextField) arg0; String text = textField.getText(); if (!isNumber(text)) { textField.setText(""); resultLabel.setText(""); if (!text.isEmpty()) { throw new UserInputException( "Must be a valid number (fraction sign ".")"); } } return isNumber(text); } }); } private void initializeComponents() { setTitle("FOREX"); setResizable(false); JPanel rootPanel = new JPanel(new GridLayout(4, 1)); JPanel amountPanel = new JPanel(); amountLabel = new JLabel("Amount"); amountPanel.add(amountLabel); amountTextBox = new JTextField(10); amountPanel.add(amountTextBox); rootPanel.add(amountPanel); JPanel currencyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); fromCurrencyLabel = new JLabel("From"); currencyPanel.add(fromCurrencyLabel); fromCurrencyCombobox = new JComboBox(Currency.values()); currencyPanel.add(fromCurrencyCombobox); toCurrencyLabel = new JLabel("To"); currencyPanel.add(toCurrencyLabel); toCurrencyCombobox = new JComboBox(Currency.values()); currencyPanel.add(toCurrencyCombobox); rootPanel.add(currencyPanel); convertButton = new JButton("Convert"); rootPanel.add(convertButton); JPanel resultPanel = new JPanel(); resultLabel = new JLabel(); resultPanel.add(resultLabel); rootPanel.add(resultPanel); add(rootPanel); } private boolean isNumber(String text) { try { Double.parseDouble(text); return true; } catch (NumberFormatException e) { return false; } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote