Question: package calc; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.GridLayout; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.BoxLayout; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class CalcApp
package calc;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CalcApp
{
private JFrame frame;
private static JTextField textField;
private double total1 ;
private double total2 = 0.0;
private double total = 0.0;
private char math_operator;
protected String btnText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcApp window = new CalcApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CalcApp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
frame.getContentPane().add(splitPane);
JPanel panel = new JPanel();
splitPane.setRightComponent(panel);
panel.setLayout(new GridLayout(5, 4, 0, 0));
JPanel panel_1 = new JPanel();
splitPane.setLeftComponent(panel_1);
setupButtons(panel);
textField = new JTextField();
panel_1.add(textField);
textField.setColumns(55);
}
private void setupButtons(JPanel panel){
JButton clearEntryButton = new JButton("CE");
clearEntryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(clearEntryButton);
JButton clearAllButton = new JButton("C");
clearAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
panel.add(clearAllButton);
JButton backspaceButton = new JButton("Backspace");
backspaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(backspaceButton);
JButton divideButton = new JButton("/");
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total = Double.parseDouble(textField.getText( ) ) ;
total1 = total1 / total;
textField.setText("");
}
});
panel.add(divideButton);
JButton button7 = new JButton("7");
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"7");
}
});
panel.add(button7);
JButton button8 = new JButton("8");
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"8");
}
});
panel.add(button8);
JButton button9 = new JButton("9");
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"9");
}
});
panel.add(button9);
JButton buttonMultiply = new JButton("X");
buttonMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total1 = total1 * Double.parseDouble(textField.getText( ) ) ;
textField.setText("");
}
});
panel.add(buttonMultiply);
JButton button4 = new JButton("4");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"4");
}
});
panel.add(button4);
JButton button5 = new JButton("5");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"5");
}
});
panel.add(button5);
JButton button6 = new JButton("6");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"6");
}
});
panel.add(button6);
JButton buttonSubtract = new JButton("-");
buttonSubtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total1 = total1 - Double.parseDouble(textField.getText( ) ) ;
textField.setText("");
}
});
panel.add(buttonSubtract);
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"1");
}
});
panel.add(button1);
JButton button2 = new JButton("2");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"2");
}
});
panel.add(button2);
JButton button3 = new JButton("3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"3");
}
});
panel.add(button3);
JButton buttonAddition = new JButton("+");
buttonAddition.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total1 = total1 + Double.parseDouble(textField.getText( ) ) ;
textField.setText("");
}
});
panel.add(buttonAddition);
JButton buttonPlusNegative = new JButton("+ -");
buttonPlusNegative.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total2 = total1 - Double.parseDouble(textField.getText( ) ) ;
textField.setText( Double.toString(total2) );
total1 = 0;
}
});
panel.add(buttonPlusNegative);
JButton button0 = new JButton("0");
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"0");
}
});
panel.add(button0);
JButton buttonDecimal = new JButton(".");
buttonDecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+".");
}
});
panel.add(buttonDecimal);
JButton buttonEquals = new JButton("=");
buttonEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total2 = total1 + Double.parseDouble(textField.getText( ) ) ;
textField.setText( Double.toString(total2) );
total1 = 0;
}
});
panel.add(buttonEquals);
}
}
how to get multiplication, subtraction, and division and backspace to work in this calculator code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
