Write a GUI program (JFrame) that lets users enter two numbers, x and y, and sel
ID: 671941 • Letter: W
Question
Write a GUI program (JFrame) that lets users enter two numbers, x and y, and select an arithmetic operator from a group of radio buttons. The program also contains a JLabel that displays related text message with GUI Calculator as the default message. For this homework, you need to write corresponding codes to handle possible exceptions. For example, if users enter texts instead of numbers in the text fields 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 thrown exceptions and display suggestions in the JLabel so users can correct input errors and continue to perform calculation. If users click the calculate button without selecting an arithmetic operator, the JLabel will display a reminder message as shown in Figure 1. If users enter valid inputs and then click the calculate button, it will display the result of x operator y. For example, if the user select the / button, the output result should be in the form of x/y = z, as shown in Figure 2. If users click the calculate button but fail to enter a number in either the x or y text field, the JLabel will display a message informing users that x and y must be numbers, see Figure 3. If users click the reset button, it will clear all the displayed text fields and reset the JLabel message to GUI Calculator if it has been altered to a different message, see Figure 4. Additional requirements: Add a title border to the JFrame with the text, GUI Calculator. JLabel: align text to the center; use a line border with a specified color and line thickness. Text Fields: align text to the right; apply a customized Font and use a font color other than black to the text fields. For example, bold style and a font size bigger than the default. Radio buttons: use radio buttons for math operators and these four radio buttons need to be grouped together so users can only choose one of them. Apply a customized Font to radio buttons so the math operators are noticeable. Note that (1) a group of radio buttons can have no initial selection, and once the user has made a selection, exactly one button is selected from then on; (2) the ButtonGroup takes care of unselecting the previously selected button when the user selects another button in the group.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.