// i need a simple calculator, see my code below and plz use the original format
ID: 3627113 • Letter: #
Question
// i need a simple calculator, see my code below and plz use the original format.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyGuiApplet extends JApplet
// implements ActionListener
{
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bdp,beq,bplus,bmin,bmul,bdiv;
JPanel p1;
JTextField t1;
public void init()
{
t1 = new JTextField(20);
p1 = new JPanel();
p1.setLayout(new GridLayout(4,4));
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");
bdp = new JButton(".");
beq = new JButton("=");
bplus = new JButton("+");
bmin = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
setLayout(new BorderLayout());
p1.add(b7);p1.add(b8);p1.add(b9);p1.add(bplus);
p1.add(b4);p1.add(b5);p1.add(b6);p1.add(bmin);
p1.add(b1);p1.add(b2);p1.add(b3);p1.add(bmul);
p1.add(b0);p1.add(bdp);p1.add(beq);p1.add(bdiv);
add("North",t1);add("Center",p1);
}
}
Explanation / Answer
Please Rate, Thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyGuiApplet extends JApplet implements ActionListener{
private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bdp, beq, bplus, bmin, bmul, bdiv;
private JPanel p1;
private JTextField t1;
private double _number;
private String _operation;
private boolean _nextNumber;
private boolean _set;
public void init() {
t1 = new JTextField(20);
t1.setHorizontalAlignment(JTextField.RIGHT);
p1 = new JPanel();
p1.setLayout(new GridLayout(4, 4));
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");
bdp = new JButton(".");
beq = new JButton("=");
bplus = new JButton("+");
bmin = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
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);
bdp.addActionListener(this);
beq.addActionListener(this);
bplus.addActionListener(this);
bmin.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
setLayout(new BorderLayout());
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(bplus);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(bmin);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(bmul);
p1.add(b0);
p1.add(bdp);
p1.add(beq);
p1.add(bdiv);
add("North", t1);
add("Center", p1);
_operation = null;
_nextNumber = false;
_set = false;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(b0)){
if (!_nextNumber){
t1.setText(t1.getText() + "0");
}else{
t1.setText("" + "0");
_nextNumber = false;
}
}else if (e.getSource().equals(b1)){
if (!_nextNumber){
t1.setText(t1.getText() + "1");
}else{
t1.setText("" + "1");
_nextNumber = false;
}
}else if (e.getSource().equals(b2)){
if (!_nextNumber){
t1.setText(t1.getText() + "2");
}else{
t1.setText("" + "2");
_nextNumber = false;
}
}else if (e.getSource().equals(b3)){
if (!_nextNumber){
t1.setText(t1.getText() + "3");
}else{
t1.setText("" + "3");
_nextNumber = false;
}
}else if (e.getSource().equals(b4)){
if (!_nextNumber){
t1.setText(t1.getText() + "4");
}else{
t1.setText("" + "4");
_nextNumber = false;
}
}else if (e.getSource().equals(b5)){
if (!_nextNumber){
t1.setText(t1.getText() + "5");
}else{
t1.setText("" + "5");
_nextNumber = false;
}
}else if (e.getSource().equals(b6)){
if (!_nextNumber){
t1.setText(t1.getText() + "6");
}else{
t1.setText("" + "6");
_nextNumber = false;
}
}else if (e.getSource().equals(b7)){
if (!_nextNumber){
t1.setText(t1.getText() + "7");
}else{
t1.setText("" + "7");
_nextNumber = false;
}
}else if (e.getSource().equals(b8)){
if (!_nextNumber){
t1.setText(t1.getText() + "8");
}else{
t1.setText("" + "8");
_nextNumber = false;
}
}else if (e.getSource().equals(b9)){
if (!_nextNumber){
t1.setText(t1.getText() + "9");
}else{
t1.setText("" + "9");
_nextNumber = false;
}
}else if (e.getSource().equals(bdp)){
if (!_nextNumber && !t1.getText().contains(".")){
t1.setText(t1.getText() + ".");
}else if (_nextNumber){
t1.setText("" + ".");
_nextNumber = false;
}
}else if (e.getSource().equals(beq)){
if(_operation != null && _set && !_nextNumber){
equal();
_operation = null;
}
}else if (e.getSource().equals(bplus)){
if(_operation != null && _set && !_nextNumber){
equal();
}else if (!t1.getText().equals("")){
_number = Double.parseDouble(t1.getText());
_set = true;
_nextNumber = true;
}
_operation = "+";
}else if (e.getSource().equals(bmin)){
if(_operation != null && _set && !_nextNumber){
equal();
}else if (!t1.getText().equals("")){
_number = Double.parseDouble(t1.getText());
_set = true;
_nextNumber = true;
}
_operation = "-";
}else if (e.getSource().equals(bmul)){
if(_operation != null && _set && !_nextNumber){
equal();
}else if (!t1.getText().equals("")){
_number = Double.parseDouble(t1.getText());
_set = true;
_nextNumber = true;
}
_operation = "*";
}else if (e.getSource().equals(bdiv)){
if(_operation != null && _set && !_nextNumber){
equal();
}else if (!t1.getText().equals("")){
_number = Double.parseDouble(t1.getText());
_set = true;
_nextNumber = true;
}
_operation = "/";
}
}
public void equal(){
if (_operation.equals("+")) {
_number = _number + Double.parseDouble(t1.getText());
} else if (_operation.equals("-")) {
_number = _number - Double.parseDouble(t1.getText());
} else if (_operation.equals("*")) {
_number = _number * Double.parseDouble(t1.getText());
} else if (_operation.equals("/")) {
_number = _number / Double.parseDouble(t1.getText());
}
t1.setText(Double.toString(_number));
_nextNumber = true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.