Question: Create an application named TaxCalc.java that allows you to enter the amount of a purchase and then displays the amount of sales tax on that
Create an application named TaxCalc.java that allows you to enter the amount of a purchase and then displays the amount of sales tax on that purchase. Use a slider to adjust the tax rate between 0 percent and 10 percent. This is what I have but there are about 4 errors that I can't figure out how to fix. Please help!!
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.text.DecimalFormat;
public class ScrollableTaxCalculator extends JFrame { private JLabel l1, l2, l3; private JTextField costA; //Price Amount private JTextField sTax; private JTextField totalSales; private JPanel ppanel; private JPanel spanel; private JPanel tpanel; private JPanel sliderPanel; private JSlider slider;
public ScrollableTaxCalculator() { setTitle("Scrollable Tax Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("Enter the amount of Price Amount: "); l2 = new JLabel("Sales Tax on the purchase: "); l3 = new JLabel("Total Sales:");
costA = new JTextField("0.0", 10); costA.setEditable(true); sTax = new JTextField("0.0", 10); sTax.setEditable(false); totalSales = new JTextField("0.0", 10); totalSales.setEditable(false);
slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 0); slider.setMajorTickSpacing(1); slider.setMinorTickSpacing(1); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.addChangeListener(new SliderListener());
ppanel = new JPanel(); ppanel.add(l1); ppanel.add(costA); spanel = new JPanel(); spanel.add(l2); spanel.add(sTax); tpanel = new JPanel(); tpanel.add(l3); tpanel.add(totalSales); sliderPanel = new JPanel(); sliderPanel.add(slider);
setLayout(new GridLayout(4, 1));
add(ppanel); add(spanel); add(tpanel); add(sliderPanel);
pack(); setVisible(true); }
private class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent e) { double price, tax, total; DecimalFormat fmt = new DecimalFormat("0.0");
price = Double.parseDouble(costA.getText()); tax = slider.getValue(); total = price * (1 + (tax/100)); costA.setText(Double.toString(price)); sTax.setText(fmt.format(tax)); totalSales.setText(fmt.format(total)); } }
public static void main(String[] args) { new ScrollableTaxCalculator(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
