Create a GUI that prompts the user to enter a measurement in inches. Calculate t
ID: 3660396 • Letter: C
Question
Create a GUI that prompts the user to enter a measurement in inches. Calculate the corresponding measurement in centimeters and display it in a text area. Continue to accept input from the user, performing the calculation and appending the output to text area, until the user presses an exit button or the program has processed 15 numbers. Before processing the 16th number, clear the text area. a sample addition to the text area might be 2.00 inches is 5.08 centimeters. Information given: For solving this problem involving the Java graphics, you will need the following static declarations and GUI components private static int WIDTH = 550; private static int HEIGHT = 320; private static double CENTIMETERS_IN_INCH = 2.54; private int row = 15; private int col = 20; private int lineCount; //GUI components private JTextField numberTF; private JTextArea displayTA; private JLabel numberL; private JButton exitB, appendB; private ButtonEventHandler eventHandler; (MUST COMPILE IN jGRASP, NO OTHER PROGRAM)Explanation / Answer
InchesToCms.java
import java.awt.FlowLayout;
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.JTextArea;
import javax.swing.JTextField;
public class InchesToCms {
private static int WIDTH = 550;
private static int HEIGHT = 320;
private static double CENTIMETERS_IN_INCH = 2.54;
private int row = 15;
private int col = 20;
private int lineCount;
// GUI components
private JTextField numberTF;
private JTextArea displayTA;
private JLabel numberL;
private JButton exitB, appendB;
private JFrame frame;
public InchesToCms() {
frame = new JFrame("Inches to cms");
numberTF = new JTextField(10);
displayTA = new JTextArea(row,col);
exitB = new JButton("Exit");
appendB = new JButton("Append");
numberL = new JLabel("enter measurement in inches:");
frame.add(numberL);
frame.add(numberTF);
frame.add(displayTA);
frame.add(exitB);
frame.add(appendB);
frame.setSize(WIDTH, HEIGHT);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
lineCount =0;
exitB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
frame.dispose();
}
});
appendB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
double inches = Double.parseDouble(numberTF.getText());
double cms = inches * CENTIMETERS_IN_INCH;
if(lineCount == 15) {
displayTA.setText("");
lineCount = 0;
}
else {
lineCount++;
displayTA.setText(displayTA.getText()+ inches + " inches is " + cms + " centimeters ");
}
}
});
}
public static void main(String[] args) {
new InchesToCms();
}
}
output: