(happy may day) intermediate calculator JAVA Swing (GUI) Jframe

JAVA Swing (GUI) intermediate calculator Jframe

Preparation operation will not, you can see the previous article: the production of simple calculator http://t.csdn.cn/mnICs
It contains the installation of WindowBuilder, the creation of JFrame and the basic creation steps

function

With memory function, it can calculate multiple values. Every two numbers will be saved in text2 until the end of the final calculation. You can rewind the data, clear the data, and close the window.
After one formula is calculated, you can directly calculate the next formula without clicking empty;
When the divisor is 0 or there are two decimal points in a number, a warning window will appear to prompt the user;

Interface display

When looking at the code, you will find that there are different situations in the code blocks that should be similar, or the functions are repeated, but the ultimate goal is the same. This is because I thought of some different methods when thinking, so I used different methods to write some similar modules. You can choose by yourself in the process of writing.

Special reminder: the length of the code is long, and some codes will be redundant, but in fact, after optimization, it can be reduced by about one or two hundred lines. This article will not be optimized. We mainly focus on understanding the thinking and methods of writing.

Main ideas

For each operation, the operation results shall be saved for the next calculation.
Key: since one data, one operator and one data appear, it is necessary to wait for two data to appear before calculating them. Here, take the second operator as the sign and take the previous operator to calculate two numbers. Therefore, it is necessary to save the previous operator with a variable.
And every time you encounter an operator, you need to clear text1 for the input of the next value. Choose = false indicates that text1 and text2 do not need to be cleared, and point=false indicates that there is no decimal point in the operation; When the formula is completed, choose will become true, so that the next number can be directly input and automatically cleared without clicking empty, and then the operation of the next formula can be carried out;
For more details, you can see the code comments for understanding; If you annotate one of many similar codes, you won't annotate them one by one;

The code is as follows
package calculator;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Calculator extends JFrame {

	private JPanel contentPane;
	private JTextField text1;
	private JTextField text2;
	private boolean chose=false,point;//Choose = false indicates that text1 and text2 do not need to be cleared, and point=false indicates that there is no decimal point in the operation;
	private String fuhao,fuhao1;
	private double count,count2,count3;//count3 is used to load the value in text2, so as to save the result of previous operation and carry out memory operation
	//count2 is used to hold the last operand
	//count is used to divide the last entered number
	private boolean f=true;//Used to determine whether it is the first operator

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Calculator frame = new Calculator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Calculator() {
		setTitle("\u8BA1\u7B97\u5668");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 490);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		text1 = new JTextField();
		text1.setFont(new Font("Chinese regular script", Font.BOLD, 20));
		text1.setBounds(31, 36, 200, 50);
		contentPane.add(text1);
		text1.setColumns(10);
		
		text2 = new JTextField();
		text2.setFont(new Font("Chinese regular script", Font.BOLD, 20));
		text2.setBounds(31, 124, 200, 50);
		contentPane.add(text2);
		text2.setColumns(10);
		
		JButton Buttondelete = new JButton("\u6E05\u7A7A");
		Buttondelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//empty
				text1.setText("");
				text2.setText("");
				f=true;//Reinitialize f to true so that the system can recognize the first operation symbol again for correct calculation;
			}
		});
		Buttondelete.setFont(new Font("Hua Wenxin Wei", Font.PLAIN, 30));
		Buttondelete.setBounds(360, 36, 100, 50);
		contentPane.add(Buttondelete);
		
		JButton Buttonexit = new JButton("\u5173\u95ED");
		Buttonexit.setFont(new Font("Hua Wenxin Wei", Font.PLAIN, 30));
		Buttonexit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);//Exit calculator window
			}
		});
		Buttonexit.setBounds(360, 124, 100, 50);
		contentPane.add(Buttonexit);
		
		JButton Button_1 = new JButton("1");
		Button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {//When choose is true, clear text1 and text2;
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_1.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_1.setBounds(30, 200, 100, 50);
		contentPane.add(Button_1);
		
		JButton Button_2 = new JButton("2");
		Button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_2.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_2.setBounds(140, 200, 100, 50);
		contentPane.add(Button_2);
		
		JButton Button_3 = new JButton("3");
		Button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_3.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_3.setBounds(250, 200, 100, 50);
		contentPane.add(Button_3);
		
		JButton Button_add = new JButton("+");
		Button_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count=Double.parseDouble(text1.getText());
				text1.setText("");
				fuhao=e.getActionCommand();
				if(f==true)//Determine whether it is the first operation symbol
				{
					fuhao1=fuhao;//In fact, it can also be changed to fuhao1 = "+", because count+0=count itself;
					f=false;
				}
				if((text2.getText()+"")=="") {
					count3=0;//When text1 is empty, only text2 in text1 is supported;
					//When count3=0, the following operations can keep count unchanged and directly assign it to text2;
				}
				else {
					count3=Double.parseDouble(text2.getText());
				}
				switch(fuhao1) {//Since the operation requires two values to be calculated, the previous operator of the current operator is used for calculation
				//If the current operator is the first operator, the current operator will be taken directly (in fact, as long as the value in the first text1 is smoothly transferred to text2)
				case "+": text2.setText(count3+count+"");count3+=count;break;
				case "-": text2.setText(count3-count+"");count3-=count;break;
				case "*": text2.setText(count3*count+"");count3*=count;break;
				case "/": if(count==0) {
					JOptionPane.showMessageDialog(null,"Divisor cannot be 0","warning",JOptionPane.ERROR_MESSAGE);
					return;
				}
				text2.setText(count3/count+"");count3/=count;
				}
				fuhao1=fuhao;//The obtained symbol is given to fuhao1. When the next value is input, the operator is used for calculation
			}
		});
		Button_add.setFont(new Font("Chinese regular script", Font.BOLD, 35));
		Button_add.setBounds(360, 200, 100, 50);
		contentPane.add(Button_add);
		
		JButton Button_4 = new JButton("4");
		Button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_4.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_4.setBounds(30, 260, 100, 50);
		contentPane.add(Button_4);
		
		JButton Button_5 = new JButton("5");
		Button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_5.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_5.setBounds(140, 260, 100, 50);
		contentPane.add(Button_5);
		
		JButton Button_6 = new JButton("6");
		Button_6.setFont(new Font("Song style", Font.BOLD, 30));
		Button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_6.setBounds(250, 260, 100, 50);
		contentPane.add(Button_6);
		
		JButton Button_subtract = new JButton("-");
		Button_subtract.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count=Double.parseDouble(text1.getText());
				text1.setText("");
				fuhao=e.getActionCommand();
				if(f==true)
				{
					fuhao1="+";
					f=false;
				}
				if((text2.getText()+"")=="") {
					count3=0;
				}
				else {
					count3=Double.parseDouble(text2.getText());
				}
				switch(fuhao1) {
				case "+": text2.setText(count3+count+"");count3+=count;break;
				case "-": text2.setText(count3-count+"");count3-=count;break;
				case "*": text2.setText(count3*count+"");count3*=count;break;
				case "/": if(count==0) {
					JOptionPane.showMessageDialog(null,"Divisor cannot be 0","warning",JOptionPane.ERROR_MESSAGE);
					return;
				}
					text2.setText(count3-count+"");count3-=count;
				}
				fuhao1=fuhao;
			}
		});
		Button_subtract.setFont(new Font("Chinese regular script", Font.BOLD, 50));
		Button_subtract.setBounds(360, 260, 100, 50);
		contentPane.add(Button_subtract);
		
		JButton Button_7 = new JButton("7");
		Button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_7.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_7.setBounds(30, 320, 100, 50);
		contentPane.add(Button_7);
		
		JButton Button_8 = new JButton("8");
		Button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_8.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_8.setBounds(140, 320, 100, 50);
		contentPane.add(Button_8);
		
		JButton Button_9 = new JButton("9");
		Button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_9.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_9.setBounds(250, 320, 100, 50);
		contentPane.add(Button_9);
		
		JButton Button_multiply = new JButton("*");
		Button_multiply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count=Double.parseDouble(text1.getText());
				text1.setText("");
				fuhao=e.getActionCommand();
				if(f==true)
				{
					fuhao1=fuhao;
					f=false;
				}
				if((text2.getText()+"")=="") {
					count3=1;
				}
				else {
					count3=Double.parseDouble(text2.getText());
				}
				switch(fuhao1) {
				case "+": text2.setText(count3+count+"");count3+=count;break;
				case "-": text2.setText(count3-count+"");count3-=count;break;
				case "*": text2.setText(count3*count+"");count3*=count;break;
				case "/": if(count==0) {
					JOptionPane.showMessageDialog(null,"Divisor cannot be 0","warning",JOptionPane.ERROR_MESSAGE);
					return;
				}
				text2.setText(count3/count+"");count3/=count;
				}
				fuhao1=fuhao;
			}
		});
		Button_multiply.setFont(new Font("Chinese regular script", Font.BOLD, 35));
		Button_multiply.setBounds(360, 320, 100, 50);
		contentPane.add(Button_multiply);
		
		JButton Button_point = new JButton(".");
		Button_point.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//Prevent the operator from entering multiple decimal points, resulting in system crash;
				point=true;
				for(int i=0;i<text1.getText().length();i++) {
					if('.'==text1.getText().charAt(i)) {
						point=false;
						JOptionPane.showMessageDialog(null,"There can only be one decimal point in a number","warning",JOptionPane.ERROR_MESSAGE);//The pop-up box warns that there can only be one decimal point in a number;
						break;
					}
				}
				if(point==true) {
					text1.setText(text1.getText()+".");
				}
			}
		});
		Button_point.setFont(new Font("Chinese regular script", Font.BOLD, 40));
		Button_point.setBounds(30, 380, 100, 50);
		contentPane.add(Button_point);
		
		JButton Button_0 = new JButton("0");
		Button_0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chose==true) {
					text1.setText("");
					text2.setText("");
				}
				text1.setText(text1.getText()+""+e.getActionCommand());
				chose=false;
			}
		});
		Button_0.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_0.setBounds(140, 380, 100, 50);
		contentPane.add(Button_0);
		
		JButton Button_result = new JButton("=");
		Button_result.setFont(new Font("Chinese regular script", Font.BOLD, 35));
		Button_result.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count2=Double.parseDouble(text1.getText());
				text1.setText(count3+" "+fuhao+" "+count2+"=");
				switch(fuhao) {
				case "+": text2.setText(count3+count2+"");break;
				case "-": text2.setText(count3-count2+"");break;
				case "*": text2.setText(count3*count2+"");break;
				case "/": if(count2==0) {
					JOptionPane.showMessageDialog(null,"Divisor cannot be 0","warning",JOptionPane.ERROR_MESSAGE);
					return;
				}
				text2.setText(count3/count2+"");
				}
				chose=true;//Click equal to change choose to true to directly perform the next operation. Because text1 and text2 are cleared after entering a number;
			}
		});
		Button_result.setBounds(250, 380, 100, 50);
		contentPane.add(Button_result);
		
		JButton Button_divide = new JButton("/");
		Button_divide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				count=Double.parseDouble(text1.getText());
				text1.setText("");
				fuhao=e.getActionCommand();
				boolean n=false;
				if(f==true)
				{
					n=true;//Indicates that the symbol is the first operator
					fuhao1=fuhao;
					f=false;
				}
				if((text2.getText()+"")=="") {
					count3=1;
				}
				else {
					count3=Double.parseDouble(text2.getText());
				}
				switch(fuhao1) {
				case "+": text2.setText(count3+count+"");count3+=count;break;
				case "-": text2.setText(count3-count+"");count3-=count;break;
				case "*": text2.setText(count3*count+"");count3*=count;break;
				case "/": if(count==0) {
					JOptionPane.showMessageDialog(null,"Divisor cannot be 0","warning",JOptionPane.ERROR_MESSAGE);//Pop up prompt will be given;
					return;
				}
				if(n==true)//When / is the operator, count is directly assigned to count3; Failure to do so will result in an error in the value given to text2 (becoming the reciprocal), for example, 5 will become 1 / 5;
				{
					text2.setText(count+"");count3=count;
				}
				else {
					text2.setText(count3/count+"");count3/=count;
				}
				}
				fuhao1=fuhao;
			}
		});
		Button_divide.setFont(new Font("Chinese regular script", Font.BOLD, 30));
		Button_divide.setBounds(360, 380, 100, 50);
		contentPane.add(Button_divide);
		
		JLabel lblNewLabel = new JLabel("\u7ED3\u679C");
		lblNewLabel.setFont(new Font("Hua Wenxin Wei", Font.PLAIN, 30));
		lblNewLabel.setBounds(271, 124, 100, 50);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("\u56DE\u9000");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//Back one space
				String s = text1.getText();
				text1.setText("");
				for(int i=0;i<s.length()-1;i++) {
					char a=s.charAt(i);
					text1.setText(text1.getText()+a);//Take out the content that has been added before + the characters that follow each time, and then store (update)
				}
			}
		});
		btnNewButton.setFont(new Font("Hua Wenxin Wei", Font.PLAIN, 30));
		btnNewButton.setBounds(250, 36, 100, 50);
		contentPane.add(btnNewButton);
	}
}

ending

Hope to help you!!!
If you have any questions, please leave a message below. Thank you

Tags: Java

Posted by Nathan__02 on Sun, 01 May 2022 15:43:01 +0300