Complete the program which follows to add code for the following three listeners
ID: 3537104 • Letter: C
Question
Complete the program which follows to add code for the following three listeners:
a) An ononymous action listener to use the newly entered number to update the label containing the
converted omount (note there is o privote method in this code to help you).
b) Add an inner class item listener that wilt updote the convertTo lobel to show the tert "Convert Rdte = " pl
conversion rate firom the supplied orroy. The combo box has a method getSelectedlndex$.
c) An anonymous closs whtch implements ONLY the mouseClicked method and updates the lobel containing
converted amount like in (A) above.
import java.awt.";
import java. awt.event. ";
import javax.swing.*;
public class ConvertDollars extends JFrame {
String[] country = {"USA", "Hong Kong", 'UK"};
doub lefi conversion = {0.97 222, 7 .54107, 0.651 95}; int currentlndex = 0;
JComboBox jcbCountry; J Label jlbConvertRate; JLabel jlbConvertsTo; JTextField jtfDollars;
public ConvertDollarsQ {
JPanel top = new JPanel(new GridLayout(1,2)); JLabeljlbEnterDollars = new JLabel("Enter Dollars: "); jtfDollars = new JTextField("1.00");
/A) add an anonymous action listener to use the newty entered number to update the label containing the converted amount (note there is a private method in this code to helP You).
top.add(lbEnterDollars); toP.addUtfDollars);
add(toP, BorderLaYout. NORTH );
jcbCountry= new JComboBox(country);
add(cbCountry, Borderlayout.CE NTER);
11B);dd an inner class item Iistener that witl update the convertTo label to show the text ,,Convert Rate = " plus the conversion rate from the supplied array. The combo box has a method getSelectedlndex0.
JPanel bottom = new JPanel(new GridLayout(3,1)); jlbConvertRate = new JLabel("Convert Rate = " + conversion[0]); jlbConvertsTo = newJLabel("ln CDN$= " + conversionl0]); JButton jbtConvert = new JButton("Convert");
bottom.addfi lbConvertRate);
botto m. ad d(j lbConvertsTo); bottom.add(jbtConvert);
add(bottom, BorderlaYout.SOUTH );
, ttal an anonymous class which imptements ONLY the mouseClicked method and updates the labelcontaining the converted amount like in (A) above.
pack0; )
private void changeConvertNumberQ {
double amount = Double. parseDouble(itfDollars. getText0);
double convertAmount = amount * conversion[icbCountry.getSelected lndexO]; jlbConvertsTo.setText(String.format("ln CDN$ = o/o5.2f', convertAmount));
)
public static void main(Stringfi args) {
ConvertDollars frame = new ConvertDollars0; frame.setTitle("Canadian Dollar Converter"); frame.setLocationRelativeTo(nul I );
f rame. setDefaultCloseOperation(J Frame. EXIT-ON-CLOS E ); frame.setVisi ble(true);
)
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConvertDollars extends JFrame {
String[] country = {"USA", "Hong Kong", "UK"};
double[] conversion = {0.97222, 7.54107, 0.65195};
int currentlndex = 0;
JComboBox jcbCountry;
JLabel jlbConvertRate;
JLabel jlbConvertsTo;
JTextField jtfDollars;
public ConvertDollars() {
JPanel top = new JPanel(new GridLayout(1,2));
JLabel jlbEnterDollars = new JLabel("Enter Dollars: ");
jtfDollars = new JTextField("1.00");
top.add(jlbEnterDollars);
top.add(jtfDollars);
add(top, BorderLayout. NORTH );
jcbCountry= new JComboBox(country);
add(jcbCountry, BorderLayout.CENTER);
JPanel bottom = new JPanel(new GridLayout(3,1));
jlbConvertRate = new JLabel("Convert Rate = " + conversion[0]);
jlbConvertsTo = new JLabel("In CDN$= " + conversion[0]);
JButton jbtConvert = new JButton("Convert");
bottom.add(jlbConvertRate);
bottom.add(jlbConvertsTo);
bottom.add(jbtConvert);
add(bottom, BorderLayout.SOUTH );
/* add an anonymous action listener to use the newty entered number to update the label containing the converted amount (note there is a private method in this code to help You). */
jbtConvert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeConvertNumber();
}
} );
/* add an inner class item Iistener that witl update the convertTo label to show the text ,,Convert Rate = " plus the conversion rate from the supplied array. The combo box has a method getSelectedlndex */
jcbCountry.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
//getItem returns an object so it gets cast
//as a String to retrieve the item value
jlbConvertRate.setText("Convert Rate = " +conversion[jcbCountry.getSelectedIndex()]);
}
});
/* an anonymous class which imptements ONLY the mouseClicked method and updates the labelcontaining the converted amount like in (A) above.*/
top.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e){ changeConvertNumber();}
public void mouseEntered(MouseEvent e){/*implementation goes here...*/}
public void mouseExited(MouseEvent e){/*implementation goes here...*/}
public void mousePressed(MouseEvent e){/*implementation goes here...*/}
public void mouseReleased(MouseEvent e){/*implementation goes here...*/ }
});
pack();
}
private void changeConvertNumber() {
double amount = Double.parseDouble(jtfDollars.getText());
double convertAmount = amount * conversion[jcbCountry.getSelectedIndex()];
jlbConvertsTo.setText(String.format("In CDN$ = %5.2f", convertAmount));
}
public static void main(String[] args) {
ConvertDollars frame = new ConvertDollars();
frame.setTitle("Canadian Dollar Converter");
frame.setLocationRelativeTo(null );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.