I have written a java calculator program that doesn\'t do calculations and would
ID: 3560537 • Letter: I
Question
I have written a java calculator program that doesn't do calculations and would like to have it work like a real calculator. Here is the code:
package calculator;
import javax.swing.*;
import java.awt.*;
public class Calc extends JFrame {
public Calc()
{
super.setTitle("Calculator");
new Font("SansSerif", Font.BOLD, 16);
JTextField jtext = new JTextField(" 0 ");
jtext.setSize(250,25);
jtext.setHorizontalAlignment(JTextField.RIGHT);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(jtext, BorderLayout.NORTH);
JPanel buttonsPanel = new JPanel(new GridLayout(4,4,5,5));
buttonsPanel.setBackground(Color. GRAY);
JButton JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");;
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton decimal = new JButton(".");
JButton equal = new JButton("=");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");
JButton minus = new JButton("-");
JButton add = new JButton("+");
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(divide);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(multiply);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(minus);
buttonsPanel.add(decimal);
buttonsPanel.add(zero);
buttonsPanel.add(equal);
buttonsPanel.add(add);
seven. setBackground(Color.BLACK);
seven.setForeground(Color.WHITE);
eight.setBackground(Color.WHITE);
eight.setForeground(Color.BLACK);
nine.setBackground(Color.BLACK);
nine.setForeground(Color.WHITE);
divide.setBackground(Color.WHITE);
divide.setForeground(Color.BLACK);
four.setBackground(Color.WHITE);
four.setForeground(Color.BLACK);
five.setBackground(Color.BLACK);
five.setForeground(Color.WHITE);
six.setBackground(Color.WHITE);
six.setForeground(Color.BLACK);
multiply.setBackground(Color.BLACK);
multiply.setForeground(Color.WHITE);
one.setBackground(Color.BLACK);
one.setForeground(Color.WHITE);
two.setBackground(Color.WHITE);
two.setForeground(Color.BLACK);
three.setBackground(Color.BLACK);
three.setForeground(Color.WHITE);
minus.setBackground(Color.WHITE);
minus.setForeground(Color.BLACK);
decimal.setBackground(Color.WHITE);
decimal.setForeground(Color.BLACK);
zero.setBackground(Color.BLACK);
zero.setForeground(Color.WHITE);
equal.setBackground(Color.WHITE);
equal.setForeground(Color.BLACK);
add.setBackground(Color.BLACK);
add.setForeground(Color.WHITE);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(textPanel, BorderLayout.NORTH);
mainPanel.add(buttonsPanel, BorderLayout.CENTER);
add(mainPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calc frame = new Calc();
frame.setSize(250, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
Please don't change the GUI, just add the math and number portion to it. Full points will be awarded to a working code only!