Write a JavaFX program that converts US dollars into Canadian dollars, as shown
ID: 3803524 • Letter: W
Question
Write a JavaFX program that converts US dollars into Canadian dollars, as shown in the following figure. The program lets the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One U.S. dollar is approximately 1.5 Canadian dollars. Put the labels and textfields in a GridPane and make the Canadian Dollars field non-editable. Don't worry about making the formatting perfect. You may omit the Java import statements and the main method to save time.
Convert Dollars Canadian Dolla lars to Canadian Dollars 15 ConveExplanation / Answer
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; public class usDollar extends JFrame { JLabel usLab, canLab; JTextField usTextF,canTextF ; JButton calculateBut, exitBut; CalculateButtonHandler calculateHand; ExitButtonHandler exitHand; public usDollar() { usLab = new JLabel("US Dollar", SwingConstants.LEFT); usTextF = new JTextField(10); canLab = new JLabel("Can Dollar", SwingConstants.LEFT); canTextF = new JTextField(10); calculateBut = new JButton("Calculate"); calculateHand = new CalculateButtonHandler(); calculateBut.addActionListener(calculateHand); exitBut = new JButton("Exit"); exitHand = new ExitButtonHandler(); exitBut.addActionListener(exitHand); setTitle("Final"); setLayout (new GridLayout(3, 3)); Container pane = getContentPane(); pane.add(usLab); pane.add(canLab); pane.add(usTextF); pane.add(canTextF); pane.add(calculateBut); pane.add(exitBut); canTextF.setEditable(false); setBounds(350, 350, 350, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int usDollar, canDollar; double usDoll, canDoll; usDollar = Integer.parseInt(usTextF.getText()); canDoll = usDollar * 1.5 ; DecimalFormat money = new DecimalFormat("$0.00"); canTextF.setText(money.format(canDoll)); } } private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String args[]) { usDollar Object = new usDollar(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.