Use java to implement a simple calculator

Use java to implement a simple calculator

I recently used Java to make a calculator. The functions in the menu bar were not implemented. I just wrote a layout and only implemented the calculation function. If you want to implement the menu bar function, you can implement it on the basis of this code.

Without further ado, let’s get straight to the code.

package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculate extends JFrame implements ActionListener {
private Container ct;
private JButton[] jbs;
private String[] str;
private JTextField jTextField;
//cal[0] is used to store the first input number
//cal[1] is used to store operators (+, -, *, /, %)
//cal[2] is used to store the second input number
//cal[3] is used to store results
//cal[4] is used to store the number stored in the MS key
//cal[5] is used to obtain the value to be stored (used to assign a value to cal[4])
private String[] cal = {"","","","","",""};
//_count and _count1 are always opposite. If _count is true, it means that cal[0] is being input. Otherwise, it means that the input of cal[0] is completed, and the input cal[2]
//Assign the value of cal[3] to cal[0] after clicking the equal sign
private boolean _count = true;
private boolean _count1 = false;
public calculate() {
super("calculator");
jbs = new JButton[28];
ct = this.getContentPane();
this.setBounds(100, 100, 315, 400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ct.setLayout(null);
//Menu Bar
JMenuBar jMenuBar = new JMenuBar();
JMenu jm = new JMenu("View(V)");
JMenu jm1 = new JMenu("Edit(E)");
JMenu jm2 = new JMenu("Help(H)");
jMenuBar.add(jm);
jMenuBar.add(jm1);
jMenuBar.add(jm2);
this.setJMenuBar(jMenuBar);
//output box
jTextField = new JTextField("0");
jTextField.setFont(new Font(" Times Roman ", font-bold, 25));
jTextField.setEditable(false);
jTextField.setHorizontalAlignment(JTextField.RIGHT);
jTextField.setBounds(5, 5, 285, 65);
ct.add(jTextField);
//Button 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
str = new String[]{"MC", "MR", "MS", "M+", "M-", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"};
int k = 0;
for (int i = 75; i <= 195; i = i + 40) {
for (int init = 10; init <= 290; init = init + 57) {
JButton jButton = new JButton(str[k]);
jbs[k] = jButton;
jButton.setBounds(init, i, 52, 35);
ct.add(jButton);
k++;
System.out.println(k);
}
}
for (int i = 235; i < 265; i = i + 40) {
for (int j = 10; j <= 181; j = j + 57) {
JButton jButton = new JButton(str[k]);
jButton.setBounds(j, i, 52, 35);
ct.add(jButton);
jbs[k] = jButton;
k++;
System.out.println(k);
}
}
JButton jButton = new JButton(str[k]);
jbs[k] = jButton;
k++;
System.out.println(k);
jButton.setBounds(240, 235, 52, 75);
ct.add(jButton);
JButton jButton2 = new JButton(str[k]);
System.out.println(str[k]);
jButton2.setBounds(10, 275, 109, 35);
ct.add(jButton2);
jbs[k] = jButton2;
k++;
for (int i = 275; i < 310; i = i + 40) {
for (int j = 124; j <= 181; j = j + 57) {
JButton jButton1 = new JButton(str[k]);
jButton1.setBounds(j, i, 52, 35);
ct.add(jButton1);
jbs[k] = jButton1;
k++;
}
}
for (int i = 0; i < 28; i++) {
jbs[i].addActionListener(this);
}
this.setVisible(true);
}
public static void main(String[] args) {
new calculate();
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getActionCommand().equals(".")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("0")){
method(actionEvent);
} else if (actionEvent.getActionCommand().equals("1")){
method(actionEvent);
}else if(actionEvent.getActionCommand().equals("2")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("3")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("4")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("5")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("6")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("7")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("8")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("9")){
method(actionEvent);
}else if (actionEvent.getActionCommand().equals("±")){
if(_count){
if (cal[0].contains(".")){
cal[0] = String.valueOf(Double.parseDouble(cal[0]) * (-1));
jTextField.setText(cal[0]);
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) * (-1));
jTextField.setText(cal[0]);
}
}else {
if (cal[2].contains(".")){
cal[2] = String.valueOf(Double.parseDouble(cal[2]) * (-1));
jTextField.setText(cal[2]);
}else {
cal[2] = String.valueOf(Integer.parseInt(cal[2]) * (-1));
jTextField.setText(cal[2]);
}
}
}else if(actionEvent.getActionCommand().equals("√")){
if(_count){
cal[0] = String.valueOf(Math.pow(Double.parseDouble(cal[0]),0.5));
jTextField.setText(cal[0]);
}else {
cal[2] = String.valueOf(Math.pow(Double.parseDouble(cal[2]),0.5));
jTextField.setText(cal[2]);
}
}else if(actionEvent.getActionCommand().equals("1/x")){
if(_count){
cal[0] = String.valueOf(1/Double.parseDouble(cal[0]));
jTextField.setText(cal[0]);
}else {
cal[2] = String.valueOf(1/Double.parseDouble(cal[2]));
jTextField.setText(cal[2]);
}
}else if(actionEvent.getActionCommand().equals("←")){
if(_count){
cal[0] = cal[0].substring(0,cal[0].length()-1);
if(cal[0].equals("")){
jTextField.setText("0");
}else {
jTextField.setText(cal[0]);
}
}else {
cal[2] = cal[2].substring(0,cal[2].length()-1);
if(cal[2].equals("")){
jTextField.setText("0");
}else {
jTextField.setText(cal[2]);
}
}
}else if(actionEvent.getActionCommand().equals("CE")){
if (_count){
cal[0]="";
jTextField.setText("0");
}else {
cal[2]="";
jTextField.setText("0");
}
}else if(actionEvent.getActionCommand().equals("C")){
_count = true;
cal[0]="";
cal[1]="";
cal[2]="";
cal[3]="";
jTextField.setText("0");
}else if (actionEvent.getActionCommand().equals("+")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) + Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) + Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "+";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("-")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) - Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) - Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "-";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("*")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) * Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) * Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "*";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("/")){
if (!cal[0].equals("")&&!cal[2].equals("")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[0]=String.valueOf(Double.parseDouble(cal[0]) / Double.parseDouble(cal[2]));
}else {
cal[0] = String.valueOf(Integer.parseInt(cal[0]) / Integer.parseInt(cal[2]));
}
cal[2]="";
}
cal[1] = "/";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("%")){
cal[0]=String.valueOf(Integer.parseInt(cal[0]) % Integer.parseInt(cal[2]));
cal[2]="";
cal[1] = "%";
_count=false;
jTextField.setText(cal[1]);
}else if (actionEvent.getActionCommand().equals("=")){
if(cal[0].equals("")&&cal[2].equals("")){
jTextField.setText("0");
}else if(!cal[0].equals("")&&cal[2].equals("")){
jTextField.setText(cal[0]);
}else if(!cal[0].equals("")&&!cal[2].equals("")){
if(cal[1].equals("+")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) + Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) + Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if (cal[1].equals("-")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) - Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) - Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if(cal[1].equals("*")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) * Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) * Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if (cal[1].equals("/")){
if (cal[0].contains(".") || cal[2].contains(".")){
cal[3]=String.valueOf(Double.parseDouble(cal[0]) / Double.parseDouble(cal[2]));
}else {
cal[3] = String.valueOf(Integer.parseInt(cal[0]) / Integer.parseInt(cal[2]));
}
jTextField.setText(cal[3]);
}else if (cal[1].equals("%")){
cal[3]=String.valueOf(Integer.parseInt(cal[0]) % Integer.parseInt(cal[2]));
jTextField.setText(cal[3]);
}
}
cal[0]=cal[3];
cal[2]="";
_count=true;
_count1 = true;
cal[5]=cal[0];
}else if(actionEvent.getActionCommand().equals("MC")){
cal[4]="";
}else if(actionEvent.getActionCommand().equals("MR")){
if(_count){
if(!cal[4].equals("")){
cal[0]=cal[4];
jTextField.setText(cal[0]);
}
}else {
if(!cal[4].equals("")){
cal[2]=cal[4];
jTextField.setText(cal[2]);
}
}
}else if(actionEvent.getActionCommand().equals("MS")){
if(_count){
cal[0]="";
}
cal[4]=cal[5];
System.out.println("cal[5]"+cal[5]);
System.out.println("ms:"+cal[4]);
}else if(actionEvent.getActionCommand().equals("M+")){
if(!cal[4].equals("")){
if (cal[4].contains(".") || cal[5].contains(".")){
cal[4]=String.valueOf(Double.parseDouble(cal[4]) + Double.parseDouble(cal[5]));
}else {
cal[4] = String.valueOf(Integer.parseInt(cal[4]) + Integer.parseInt(cal[5]));
}
}
}else if(actionEvent.getActionCommand().equals("M-")){
if(!cal[4].equals("")){
if (cal[4].contains(".") || cal[5].contains(".")){
cal[4]=String.valueOf(Double.parseDouble(cal[4]) - Double.parseDouble(cal[5]));
}else {
cal[4] = String.valueOf(Integer.parseInt(cal[4]) - Integer.parseInt(cal[5]));
}
}
}
}
public void method(ActionEvent actionEvent){
if(_count){
if (_count1){
cal[0]="";
_count1=false;
}
cal[0] += actionEvent.getActionCommand();
cal[5]=cal[0];
System.out.println(cal[5]);
}else {
cal[2] += actionEvent.getActionCommand();
cal[5]=cal[2];
}
jTextField.setText(actionEvent.getActionCommand());
}
}

Rendering:

If there are any errors, please correct me

Related Posts

Modify the built-in user password in elasticsearch version 8.0 or above

Java API to operate HBase

Details of automotive project based on SSM and Shiro

To merged Charts

[MC Tutorial] Launch the Java version of mc on iPad (no jailbreak required) (nanny level?) Jitterbug launches the iOS Minecraft Java version launcher PojavLauncher

springboot uses redis

SpringBoot project slimmed down from 18.18M to 0.18M

Analysis and practical use of database SQLite in Android Studio App development (including creating databases, adding, deleting, modifying, checking, remembering passwords, etc. The source code must be read)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*