Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:32:34+00:00 2026-06-15T18:32:34+00:00

The homework expands on our previous lab where we made a really basic calculator.

  • 0

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;    
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:32:34+00:00Added an answer on June 15, 2026 at 6:32 pm

    Use a layout manager that is more capable of meeting your needs, something like a GridLayout…

    enter image description here

    In your BasicCalculator class, change the keyPad to use a GridLayout and pack the window.

    keyPad = new JPanel(new GridLayout(0, 3));
    //...
    calWin.getContentPane().add(keyPad);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've done some homework on this and understand the basic rules of Version Control.
our homework is to write a ruby script who calculate a subset of wordlist
For homework I need to use IPC. I write some code for shared memory
Our homework assignment asks us to prove that the Java LinkedList implementation is doubly-linked
I have a homework assignment to do and I really need a solution. I
[Homework disclaimer] I'm working on the binary bomb lab . Basically, I have to
For homework I am supposed to create a circularly linked list using nodes and
Part of my homework for tomorrow is to search and add entries using Java
Our homework assignment asks us to use a jagged array to store the values
As part of our homework we need to map imageA into iamge B I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.