The homework expands on our previous lab where we made a really basic calculator. We are supposed to add some buttons pow, sqrt, pct, and log.
I extended from my BasicCalculator.java file but the buttons I added in my new class are not being added but the setBackground colors are working perfectly fine. so why are the buttons added in the same method not being added?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends BasicCalculator
{
// Attributes
protected JButton pow, sqr, pct, log;
// Constructors
public Calculator()
{
super();
}
// Methods
public void createModifiedUserInterface()
{
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
}
catch (Exception e) {
e.printStackTrace();
}
createUserInterface();
pow = new JButton("^");
sqr = new JButton("sqr");
pct = new JButton("pct");
log = new JButton("log");
keyPad.add(pow);
keyPad.add(sqr);
keyPad.add(log);
keyPad.add(pct);
displayBox.setHorizontalAlignment(displayBox.RIGHT);
displayBox.setBackground(Color.YELLOW);
clr.setBackground(Color.RED);
com.setBackground(Color.GREEN);
b0.setBackground(Color.YELLOW);
b1.setBackground(Color.YELLOW);
b2.setBackground(Color.YELLOW);
b3.setBackground(Color.YELLOW);
b4.setBackground(Color.YELLOW);
b5.setBackground(Color.YELLOW);
b6.setBackground(Color.YELLOW);
b7.setBackground(Color.YELLOW);
b8.setBackground(Color.YELLOW);
b9.setBackground(Color.YELLOW);
bd.setBackground(Color.YELLOW);
add.setBackground(Color.ORANGE);
sub.setBackground(Color.ORANGE);
mul.setBackground(Color.ORANGE);
div.setBackground(Color.ORANGE);
mod.setBackground(Color.ORANGE);
pow.setBackground(Color.ORANGE);
sqr.setBackground(Color.PINK);
log.setBackground(Color.PINK);
pct.setBackground(Color.PINK);
pow.addActionListener(this);
sqr.addActionListener(this);
log.addActionListener(this);
pct.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
displayBox.setBackground(Color.YELLOW);
super.actionPerformed(e);
if (e.getSource() == pow)
{
operator = '^';
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setText("");
}
if (e.getSource() == sqr)
{
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setBackground(Color.GREEN);
displayBox.setText(new Double(Math.sqrt(operand1)).toString());
}
if (e.getSource() == log)
{
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setBackground(Color.GREEN);
displayBox.setText(new Double(Math.log(operand1)).toString());
}
if (e.getSource() == pct)
{
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setBackground(Color.GREEN);
displayBox.setText(new Double(operand1 * 100).toString());
}
}
public void calculate()
{
displayBox.setBackground(Color.GREEN);
super. calculate();
if (operator == '^')
{
displayBox.setText(new Double(Math.pow(operand1, operand2)).toString());
}
}
public static void main(String[] args)
{
Calculator mine = new Calculator();
mine.createModifiedUserInterface();
}
}
here is the code that is in the ‘BasicCalculator.java’ file even though it’s pretty clunky.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BasicCalculator implements ActionListener
{
//attributes
protected double operand1, operand2;
protected char operator;
protected JFrame calWin;
protected JTextField displayBox;
protected JPanel keyPad;
protected JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, dot;
protected JButton add, sub, mul, div, mod, com, clr;
//constructors
public BasicCalculator()
{
clean();
}
//methods
public void clean()
{
operand1 = 0;
operand2 = 0;
operator = ' ';
}
public void createUserInterface()
{
calWin = new JFrame("Calculator");
displayBox = new JTextField(20);
keyPad = new JPanel();
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
dot = new JButton(".");
add = new JButton("+");
sub = new JButton("-");
mul = new JButton("*");
div = new JButton("/");
mod = new JButton("%");
com = new JButton("=");
clr = new JButton("C");
keyPad.add(displayBox);
keyPad.add(b0);
keyPad.add(b1);
keyPad.add(b2);
keyPad.add(b3);
keyPad.add(b4);
keyPad.add(b5);
keyPad.add(b6);
keyPad.add(b7);
keyPad.add(b8);
keyPad.add(b9);
keyPad.add(dot);
keyPad.add(add);
keyPad.add(sub);
keyPad.add(mul);
keyPad.add(div);
keyPad.add(mod);
keyPad.add(com);
keyPad.add(clr);
calWin.getContentPane().add(keyPad);
calWin.setSize(300, 300);
calWin.setVisible(true);
calWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
dot.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
com.addActionListener(this);
clr.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b0)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "0");
}
if(e.getSource() == b1)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "1");
}
if(e.getSource() == b2)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "2");
}
if(e.getSource() == b3)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "3");
}
if(e.getSource() == b4)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "4");
}
if(e.getSource() == b5)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "5");
}
if(e.getSource() == b6)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "6");
}
if(e.getSource() == b7)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "7");
}
if(e.getSource() == b8)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "8");
}
if(e.getSource() == b9)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + "9");
}
if(e.getSource() == dot)
{
if(displayBox.getText().contains("+") && operator == '+' ||
displayBox.getText().contains("-") && operator == '-' ||
displayBox.getText().contains("/") && operator == '/' ||
displayBox.getText().contains("*") && operator == '*' ||
displayBox.getText().contains("%") && operator == '%')
{
displayBox.setText("");
}
displayBox.setText(displayBox.getText() + ".");
}
if(e.getSource() == add)
{
operator = '+';
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setText("+ ");
}
if(e.getSource() == sub)
{
if(displayBox.getText().trim().equals("") ||
operator == '+' ||
operator == '-' ||
operator == '/' ||
operator == '*' ||
operator == '%')
{
displayBox.setText("-");
}
else
{
operator = '-';
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setText("- ");
}
}
if(e.getSource() == div)
{
operator = '/';
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setText("/ ");
}
if(e.getSource() == mul)
{
operator = '*';
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setText("* ");
}
if(e.getSource() == mod)
{
operator = '%';
operand1 = new Double(displayBox.getText()).doubleValue();
displayBox.setText("% ");
}
if(e.getSource() == clr)
{
displayBox.setText("");
clean();
}
if(e.getSource() == com)
{
operand2 = new Double(displayBox.getText()).doubleValue();
calculate();
}
}
public void calculate()
{
switch(operator)
{
case '+': displayBox.setText(new Double(operand1 + operand2).toString()); break;
case '-': displayBox.setText(new Double(operand1 - operand2).toString()); break;
case '*': displayBox.setText(new Double(operand1 * operand2).toString()); break;
case '/': displayBox.setText(new Double(operand1 / operand2).toString()); break;
case '%': displayBox.setText(new Double(operand1 % operand2).toString()); break;
}
}
}
Use a layout manager that is more capable of meeting your needs, something like a
GridLayout…In your
BasicCalculatorclass, change thekeyPadto use aGridLayoutand pack the window.