import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ConversionCalculator extends JFrame
{

private final int window_WIDTH=500;
private final int window_HEIGHT=250;

private JTextField centimeterTxt=new JTextField(10);
private JTextField inchesTxt=new JTextField(10);
private JTextField metersTxt=new JTextField(10);
private JTextField yardsTxt=new JTextField(10);

private JPanel inputPanel=new JPanel();
private JPanel buttonPanel=new JPanel();

private JButton clearBtn=new JButton("Clear");
private JButton calculateBtn=new JButton("Caclculate");
private JButton exitBtn=new JButton("Exit");

//Constructor
public ConversionCalculator()
{
//set title
setTitle("Conversion Calculator");
//set size
setSize(window_WIDTH, window_HEIGHT);
//Call createGUIPanel
createGUIPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set visible true
setVisible(true);


}


//Create a GUI panel
private void createGUIPanel()
{

JPanel guiPanel=new JPanel();
guiPanel.setLayout(new GridLayout(1, 2));


//Add controls to the input panel
inputPanel.setLayout(new GridLayout(2, 4));
inputPanel.add(new JLabel("Centimeters"));
centimeterTxt.setText("0.00");
inputPanel.add(centimeterTxt);
inputPanel.add(new JLabel("Inches"));
inchesTxt.setText("0.00");
inputPanel.add(inchesTxt);

inputPanel.add(new JLabel("Meters"));
metersTxt.setText("0.00");
inputPanel.add(metersTxt);
inputPanel.add(new JLabel("Yards"));
yardsTxt.setText("0.00");
inputPanel.add(yardsTxt);

//Add controls to the button panel
buttonPanel.setLayout(new GridLayout(3, 1));
clearBtn.addActionListener(new ClearButtonListener());
buttonPanel.add(clearBtn);
calculateBtn.addActionListener(new CalculateButtonListener());
buttonPanel.add(calculateBtn);
exitBtn.addActionListener(new ExitButtonListener());
buttonPanel.add(exitBtn);


//Add input panale to guipanel
guiPanel.add(inputPanel);
//Add button panel to the guipanel
guiPanel.add(buttonPanel);

add(guiPanel);

pack();
}

/*Inner class that implements action listener for exit button that
close the application */
private class ExitButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}

}

/*Inner class that implements action listener for calculate button that
converts the centimeters to inches and meters to yards */
private class CalculateButtonListener implements ActionListener
{

@Override
public void actionPerformed(ActionEvent e)
{
//Check if centimeters is not 0.00
if(!centimeterTxt.getText().equals("0.00"))
{
double centimeters=Double.parseDouble(centimeterTxt.getText());
//convert inches
double inches=centimeters*0.3937;
//set inches textfield
inchesTxt.setText(String.valueOf(inches));
//convert yards

}
//Check if meters is not 0.00
if(!metersTxt.getText().equals("0.00"))
{
double meters=Double.parseDouble(metersTxt.getText());
//convert yards
double yards=meters*1.0936;
//set yardTxt textfield
yardsTxt.setText(String.valueOf(yards));
//convert inches

}
}

}


/*Inner class that implements action listener for clear button that
resets the textfields to zero */
private class ClearButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
centimeterTxt.setText("0.00");
inchesTxt.setText("0.00");
metersTxt.setText("0.00");
yardsTxt.setText("0.00");
}
}

}

The former code is the current code I have. The following is what needs to be changed.

Implement the following event handling routines using Action Listeners

When the user enters 10 at the "Inches" JTextField and clicks the "Calculate" button.

a.Using the following equations, convert the entered length to other scales and then display on the corresponding .

1 inch = 2.54 cm

1 inch = 0.0278 yard

1 cm = 0.01 meter

NOTE: If you have trouble with these unit conversions and cannot get a hold of the instructor or TA, feel free to look up the conversions on Google.

b.All values must be rounded to two decimal places and then displayed. One way to accomplish this would be a DecimalFormat object.

This is the sample of the output needed

Answer :

ammary456

Answer:

public class ConversionCalculator extends JFrame {

  private JLabel inch_Label, meter_Label, cm_Label, yard_Label;

  private JButton clear, calculate, exit;

  private JTextField inch_tf, cm_tf, meters_tf, yards_tf;

 

  public ConversionCalculator()

  {

     

      exitButtonHandler exitB;

      clearButtonHandler clearB;

      calcButtonHandler calcB;

     

      setTitle("Conversion Calculator");

      setSize(600,200);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     

      //The first layout we use will be 1 row with three columns

      setLayout(new GridLayout(1,3));

     

      //initialize all the components

      inch_Label = new JLabel("Inches");

      meter_Label = new JLabel("Meters");

      cm_Label = new JLabel("Centimeters");

      yard_Label = new JLabel("Yards");

      clear = new JButton("Clear");

      calculate = new JButton("Calculate");

      exit = new JButton("Exit");

      inch_tf = new JTextField("0.00");

      cm_tf = new JTextField("0.00");

      meters_tf = new JTextField("0.00");

      yards_tf = new JTextField("0.00");

 

      JPanel panel1 = new JPanel();

      panel1.setLayout(new GridLayout(2,2));

      JPanel panel2 = new JPanel();

      panel2.setLayout(new GridLayout(2,2));

      JPanel panel3 = new JPanel();

      panel3.setLayout(new GridLayout(3,1));

      panel1.add(cm_Label);

      panel1.add(cm_tf);

      panel1.add(meter_Label);

      panel1.add(meters_tf);

     

      panel2.add(inch_Label);

      panel2.add(inch_tf);

      panel2.add(yard_Label);

      panel2.add(yards_tf);

     

      panel3.add(clear);

      panel3.add(calculate);

      panel3.add(exit);

     

      add(panel1);

      add(panel2);

      add(panel3);

     

     

      exitB = new exitButtonHandler();

      exit.addActionListener(exitB);

     

      clearB = new clearButtonHandler();

      clear.addActionListener(clearB);

     

      calcB = new calcButtonHandler();

      calculate.addActionListener(calcB);

     

      setVisible(true);

  }

 

  //public static void main(String[] args)

     // {

       // new ConversionCalculator();

      //}

 

  private class exitButtonHandler implements ActionListener

  {

      public void actionPerformed(ActionEvent e)

      {

          //exits program

          System.exit(0);

      }

  }

 

  private class clearButtonHandler implements ActionListener

  {

      public void actionPerformed(ActionEvent e)

      {

          //clear button sets all text fields to 0

          inch_tf.setText("0.00");

          meters_tf.setText("0.00");

          yards_tf.setText("0.00");

          cm_tf.setText("0.00");

      }

  }

 

  private class calcButtonHandler implements ActionListener

  {

      public void actionPerformed(ActionEvent e)

      {

          double inches, yards, meters, cms;

          DecimalFormat df = new DecimalFormat("0.00");

         

          //parse strings in textbox into doubles

          inches = Double.parseDouble(inch_tf.getText());

          yards = Double.parseDouble(yards_tf.getText());

          meters = Double.parseDouble(meters_tf.getText());

          cms = Double.parseDouble(cm_tf.getText());

         

          //we check which value has been tampered with and base our conversion off this

          //because of this it is important that the user clears, or else it will do inch conversion

          if(inches != 0.00)

          {

              cms = inches * 2.54;

              meters = cms / 100;

              yards = inches / 36;

             

              cm_tf.setText(df.format(cms));

              meters_tf.setText(df.format(meters));

              yards_tf.setText(df.format(yards));

             

          }

          else if(yards != 0.00)

          {

              inches = yards / 36;

              cms = inches * 2.54;

              meters = cms / 100;

             

              cm_tf.setText(df.format(cms));

              meters_tf.setText(df.format(meters));

              inch_tf.setText(df.format(inches));

          }

          else if(meters != 0.00)

          {

              cms = meters * 100;

              inches = cms / 2.54;

              yards = inches / 36;

             

              cm_tf.setText(df.format(cms));

              inch_tf.setText(df.format(inches));

              yards_tf.setText(df.format(yards));

          }

          else if(cms != 0.00)

          {

              inches = cms / 2.54;

              yards = inches / 36;

              meters = cms / 100;

             

              meters_tf.setText(df.format(meters));

              inch_tf.setText(df.format(inches));

              yards_tf.setText(df.format(yards));

          }

      }

  }

}

Explanation:

  • Inside the action performed method, pass the strings in text box.
  • check if the value has been modified then do the relevant conversions inside the conditional statement.
  • When the user clears, it will not do to the inch conversion.

Other Questions