Java Question (Uses swing class to create actual GUI window like displayed below
ID: 3687995 • Letter: J
Question
Java Question (Uses swing class to create actual GUI window like displayed below. It must match the one below showing name, graying out the initial calculation option, etc. See notes below.
By using the swing classes in Java, create a GUI as in the screenshot below. Dimensions of the frame; 250 width and 250 height. When you run the program, the program must look as the figure-1 below. At that time, both plus and minus buttons must be enabled. However, the Calculate button must be disabled (setEnabled(false) ). Just after one of the operator buttons (plus and minus) is clicked, the Calculate button must be enabled. When you click on plus button, plus button must be disabled and the minus button must be enabled. When you click on minus button, minus button must be disabled and the plus button must be enabled. Just after you click on Calculate button, it must display the result in the format which is in the screenshot. Submit one screenshot only, that is the figure-3 below. Your screenshot must look the same as the figure-3 below, except your full name at the bottom of frame
Number-1: Number-1 9 Number-1: 9 Number-2: Number-2: 2 Number-2 : 2 Calculate Calculate Calculate Result: Result: Result: 9+2=11 Written by Bilal Gonen Written by Bilal Gonen Written by Bilal GonenExplanation / Answer
Hello there,
Kindly find below code and o/p.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
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;
/**
* Calculator in Swing.
* @author dipal.prajapati
*
*/
public class Calculator {
JButton add, subtract, calculate;
JTextField num1st, num2nd;
JLabel answer;
String result = "";
public Calculator() {
JFrame calc = new JFrame("Calculator");
calc.setDefaultCloseOperation(calc.EXIT_ON_CLOSE);
///Panel 1
JPanel contentPane1 = new JPanel(new GridLayout(0, 2, 20, 20));
contentPane1.setBackground(Color.CYAN);
///Panel 2
JPanel contentPane2 = new JPanel(new GridLayout(0, 2, 20, 20));
contentPane2.setBackground(Color.YELLOW);
///Panel 3
JPanel contentPane3 = new JPanel();
num1st = new JTextField(2);
num2nd = new JTextField(2);
add = new JButton("+");
subtract = new JButton("-");
calculate = new JButton("Calculate");
answer = new JLabel("----");
calc.setSize(250, 250);
calc.setLayout(new GridLayout(3, 2, 1, 1));
contentPane1.add(new JLabel("Number-1:"));
contentPane1.add(num1st);
contentPane1.add(new JLabel("Number-2:"));
contentPane1.add(num2nd);
contentPane3.add(new JLabel("Result: "));
add.addActionListener(new action());
subtract.addActionListener(new action());
calculate.setEnabled(false);
calculate.addActionListener(new action());
contentPane3.add(answer);
contentPane2.add(add, new GridLayout(0, 2));
contentPane2.add(subtract, new GridLayout(0, 2));
contentPane2.add(calculate,new BorderLayout());
calc.getContentPane().add(contentPane1);
calc.getContentPane().add(contentPane2);
calc.getContentPane().add(contentPane3);
calc.setVisible(true);
}
public static void main(String[] args) {
Calculator lc = new Calculator();
}
public class action implements ActionListener {
public void actionPerformed(ActionEvent ae) {
int firstNum = Integer.parseInt(num1st.getText());
int secondNum = Integer.parseInt(num2nd.getText());
if ("+".equals(ae.getActionCommand())) {
calculate.setEnabled(true);
subtract.setEnabled(true);
add.setEnabled(false);
int cal = firstNum + secondNum;
result = firstNum + "+" + secondNum + "=" + String.valueOf(cal);
} else if ("-".equals(ae.getActionCommand())) {
calculate.setEnabled(true);
add.setEnabled(true);
subtract.setEnabled(false);
int cal = firstNum - secondNum;
result = firstNum + "-" + secondNum + "=" + String.valueOf(cal);
} else if ("Calculate".equals(ae.getActionCommand())) {
answer.setText(String.valueOf(result));
}
}
}
}
I am unable to attach an image file for O/P .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.