Write a JavaFX (GUI) application that functions as a calculator. It lets users e
ID: 3796243 • Letter: W
Question
Write a JavaFX (GUI) application that functions as a calculator. It lets users enter two numbers, x and y, and select an arithmetic operator from a group of radio buttons. When users enter valid inputs and click the calculate button, it will display the result in a textfield. When users click the reset button, it will reset all textfields. The program also contains a label that displays related text message with “GUI Calculator” as its default message if users enter texts instead of numbers in the textfields and then click the calculate button that will cause run-time errors. Instead of exiting the program abruptly, use try and catch blocks to handle such exceptions. Specifically, the catch block will catch the thrown exception and display suggestions in the message label so users can correct input errors and continue to perform calculation. If users enter valid inputs and then click the calculate button but without selecting an arithmetic operator, the message label will display a reminder message. If users enter valid inputs, select an arithmetic operator, and then click the calculate button, it will display the result of x operator y in the output textfield. For example, if the user select the “ - ” button, the output result should be in the form of x - y = z. If users click the calculate button but fail to enter a number in either the x or y textfield, the message label will display a message informing users that x and y must be numbers. If users click the reset button, it will clear all the textfields and reset the message label message to “GUI Calculator” if it has been altered to a different message.
If users enter valid inputs and then click the calculate button but without selecting an arithmetic
operator, the message label will display a reminder message.
If users enter valid inputs, select an arithmetic operator, and then click the calculate button, it will
display the result of x operator y in the output textfield.
If users click the calculate button but fail to enter a number in either the x or y textfield, the message
label will display a message informing users that x and y must be numbers
If users click the reset button, it will clear all the textfields and reset the message label message to
“GUI Calculator” if it has been altered to a different message
Please only use GUI interface application. No java.awt, javax.swing, or JOptionPane. Try to keep it as simple as possible.
Explanation / Answer
import java.awt.*; // for layout managers
import java.awt.event.*; // for ActionListener/ActionEvent
import javax.swing.*; // JPanel, JFrame
public class calculator // create a simple GUI calculator with JButtons, JLabels and JTextFields
{
public static void main(String[] args)
{
JFrame f=new JFrame("calculator");
f.setSize(550,125);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CalculatorPanel cp=new CalculatorPanel();
f.add(cp);
f.setVisible(true);
}
public static class CalculatorPanel extends JPanel implements ActionListener // for JButtons
{
private JTextField f1, f2, f3; // f1 and f2 will be for input, f3 for output (could be a JLabel)
private JButton add, sub, mul, div; // our JButtons to do the 4 arithmetic operations for the calculator
public CalculatorPanel()
{
add=new JButton("Add"); // create the JButtons
sub=new JButton("Subtract");
mul=new JButton("Multiply");
div=new JButton("Divide");
add.addActionListener(this); // add this as their ActionListeners
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
JLabel lab1=new JLabel("Enter the fisr number (x)"); // create the 3 JLabels for each of the JTextFields
JLabel lab2=new JLabel("Enter the second number (y)");
JLabel lab3=new JLabel("Output");
f1=new JTextField("",10); // create the 3 JTextFields, all initially blank
f2=new JTextField("",10);
f3=new JTextField("",10);
f3.setEditable(false); // f3 will be used for output only so is not editable (could be a JLabel)
JPanel p1=new JPanel(); // create a JPanel for the JLabels and JTextFields
p1.add(lab1);
p1.add(f1);
p1.add(lab2);
p1.add(f2);
p1.add(lab3);
p1.add(f3);
JPanel p2=new JPanel(); // create a JPanel for the 4 JButtons
p2.add(add);
p2.add(sub);
p2.add(mul);
p2.add(div);
JPanel p3=new JPanel(new GridLayout(2,1)); // add the two JPanels, one per row
p3.add(p1);
p3.add(p2);
add(p3); // add the last JPanel to this
}
public void actionPerformed(ActionEvent e) // upon a JButton press
{
double num1, num2, Output;
num1=Double.parseDouble(f1.getText()); // get the two input numbers as doubles
num2=Double.parseDouble(f2.getText());
if(e.getSource()==add) // perform the arithmetic operation based on the JButton pressed, storing Output in Output
Output=num1+num2;
else if(e.getSource()==sub)
Output=num1-num2;
else if(e.getSource()==mul)
Output=num1*num2;
else Output=num1/num2;
f3.setText(""+Output); // display the Output
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.