Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple calculator. Your interface should contain two input boxes in whi

ID: 3839059 • Letter: C

Question

Create a simple calculator. Your interface should contain two input boxes in which the user can put two numbers. There should be four buttons: add, subtract, multiply, and divide. When the user inputs two numbers and clicks on an operation button, the result should be displayed in a label.

Here is my GUI and code so far. I'm using NetBeans IDE and the programming language is Java.

public class NewJFrame extends javax.swing.JFrame {

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
int firstNum;
int secondNum;
int computedResult;
String operation;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

secondNumber = new javax.swing.JTextField();
firstNumber = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
divideBtn = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
multiplyBtn = new javax.swing.JButton();
addBtn = new javax.swing.JButton();
subtractBtn = new javax.swing.JButton();
result = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

secondNumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
secondNumberActionPerformed(evt);
}
});

firstNumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
firstNumberActionPerformed(evt);
}
});

jLabel1.setText("Enter First Number:");

jLabel2.setText("Enter Second Number:");

divideBtn.setText("Divide");
divideBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divideBtnActionPerformed(evt);
}
});

jLabel3.setText("Result:");

multiplyBtn.setText("Multiply");
multiplyBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
multiplyBtnActionPerformed(evt);
}
});

addBtn.setText("Add");
addBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addBtnActionPerformed(evt);
}
});

subtractBtn.setText("Subtract");
subtractBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subtractBtnActionPerformed(evt);
}
});

result.setText("0.0");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstNumber)
.addComponent(secondNumber)
.addComponent(result, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(divideBtn)
.addGap(18, 18, 18)
.addComponent(multiplyBtn)
.addGap(18, 18, 18)
.addComponent(addBtn)
.addGap(18, 18, 18)
.addComponent(subtractBtn)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(firstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(secondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(result, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(divideBtn)
.addComponent(multiplyBtn)
.addComponent(addBtn)
.addComponent(subtractBtn))
.addContainerGap(58, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void secondNumberActionPerformed(java.awt.event.ActionEvent evt) {   
// TODO add your handling code here:
}

private void firstNumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}   

private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {   
// TODO add your handling code here:
}

private void subtractBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}   

private void multiplyBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}   

private void divideBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}   

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify   
private javax.swing.JButton addBtn;
private javax.swing.JButton divideBtn;
private javax.swing.JTextField firstNumber;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton multiplyBtn;
private javax.swing.JLabel result;
private javax.swing.JTextField secondNumber;
private javax.swing.JButton subtractBtn;
// End of variables declaration   
}

Design Preview New JFrane D Enter First Number Enter Second Number: Result: 0.0 Multiply Divide Add Subtract

Explanation / Answer

Simple Code for calculator using JAVA Swing import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Frame extends JFrame { private double tempNumbers1 = 0; private double tempNumbers2 = 0; private byte function = -1; private JTextField resultJText; public Frame() { JButton[] numberButtons = new JButton[10]; for ( int i = 9; i >= 0; i--) { numberButtons[i] = new JButton(Integer.toString(i)); } JButton enterButton = new JButton("Enter"); JButton cButton = new JButton("C"); JButton multiplyButton = new JButton("*"); JButton divideButton = new JButton("/"); JButton addButton = new JButton("+"); JButton substractButton = new JButton("-"); resultJText = new JTextField(); resultJText.setPreferredSize(new Dimension(160, 20)); resultJText.setBackground(Color.WHITE); resultJText.setEnabled(false); resultJText.setHorizontalAlignment(4); resultJText.setDisabledTextColor(Color.BLACK); JPanel motherPanel = new JPanel(); motherPanel.setLayout(new BoxLayout(motherPanel, BoxLayout.Y_AXIS)); JPanel textPanel = new JPanel(); textPanel.setPreferredSize(new Dimension(160, 20)); textPanel.add(resultJText); JPanel numberButtonsPanel = new JPanel(); numberButtonsPanel.setPreferredSize(new Dimension(160, 100)); for(int i = 9; i>=0; i--) { numberButtonsPanel.add(numberButtons[i]); } JPanel functionButtonPanel = new JPanel(); functionButtonPanel.setPreferredSize(new Dimension(160, 35)); functionButtonPanel.add(enterButton); functionButtonPanel.add(cButton); functionButtonPanel.add(multiplyButton); functionButtonPanel.add(divideButton); functionButtonPanel.add(addButton); functionButtonPanel.add(substractButton); numberButtonsAction[] numberButtonActions = new numberButtonsAction[10]; for ( int i = 0; i < 10; i++ ) { numberButtonActions[i] = new numberButtonsAction(numberButtons[i]); numberButtons[i].addActionListener(numberButtonActions[i]); } EnterButton enter = new EnterButton(); enterButton.addActionListener(enter); CButton c = new CButton(); cButton.addActionListener(c); MultiplyButton multiply = new MultiplyButton(); multiplyButton.addActionListener(multiply); DivideButton divide = new DivideButton(); divideButton.addActionListener(divide); AddButton add = new AddButton(); addButton.addActionListener(add); SubtractButton subtract = new SubtractButton(); substractButton.addActionListener(subtract); motherPanel.add(textPanel); motherPanel.add(numberButtonsPanel); motherPanel.add(functionButtonPanel); add(motherPanel); setTitle("ButtonTest"); setSize(180, 290); setLocationByPlatform(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } private class numberButtonsAction implements ActionListener { private String c; public numberButtonsAction(JButton a) { this.c = a.getText(); } public void actionPerformed(ActionEvent e) { if (!resultJText.getText().equals("0.0")) { resultJText.setText(resultJText.getText() + c); } else { resultJText.setText(""); actionPerformed(e); } } } private class EnterButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { tempNumbers2 = Double.parseDouble(resultJText.getText()); if (function == 0) { resultJText.setText(Double.toString((Math.round((tempNumbers1 / tempNumbers2) * 100)) / 100)); } else if (function == 1) { resultJText.setText(Double.toString(tempNumbers1 * tempNumbers2)); } else if (function == 2) { resultJText.setText(Double.toString(tempNumbers2 + tempNumbers1)); } else if (function == 3) { resultJText.setText(Double.toString(tempNumbers1 - tempNumbers2)); } else { resultJText.setText(String.valueOf(tempNumbers1)); } tempNumbers1 = Double.parseDouble(resultJText.getText()); } } private class CButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { resultJText.setText(""); tempNumbers1 = 0; tempNumbers2 = 0; function = -1; } } private class DivideButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 0; } } private class MultiplyButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 1; } } private class AddButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 2; } } private class SubtractButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tempNumbers1 == 0) { tempNumbers1 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } else { tempNumbers2 = Double.parseDouble(resultJText.getText()); resultJText.setText(""); } function = 3; } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote