DIY simple calculator
Draw lessons from bloggers https://blog.csdn.net/without_scruple/article/details/78603605
This blogger's code is the most easy to understand and concise code I see. At the same time, I added the functions of clearing, naming and icon to realize simple addition, subtraction, multiplication and division
A closer understanding of the java interface
The code is as follows:
package design; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.StreamTokenizer; import java.util.StringTokenizer; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Demo1_topic01 extends JFrame { private static final long serialVersionUID = 1L; private JTextField text; public JButton jb0,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9; //Four numbers from 0 to 9 (can be written as an array) public JButton bot,add,sub,mul,div,equ,clear; //Addition, subtraction, multiplication and division, and five symbols of decimal point private JPanel panel; public Demo1_topic01() { init(); Panel(); MyEvent(); //event processing } private void MyEvent() { //Add event handling for each button Text(jb0); Text(jb1); Text(jb2); Text(jb3); Text(jb4); Text(jb5); Text(jb6); Text(jb7); Text(jb8); Text(jb9); Text(bot); Text(add); Text(sub); Text(mul); Text(div); //Event handling of "=" button equ.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { text.setText(text.getText() + equ.getText()); if(text.getText().contains("+")) {//Do addition StringTokenizer str1 = new StringTokenizer(text.getText(), "+"); double d1, d2 = 0; d1 = Double.parseDouble(str1.nextToken()); while (str1.hasMoreTokens()) { StringTokenizer str2 = new StringTokenizer(str1.nextToken(),"="); d2 = Double.parseDouble(str2.nextToken()); } text.setText(text.getText() + " " + (d1+d2));//Put the result in the text area }else if (text.getText().contains("-")) { StringTokenizer str1 = new StringTokenizer(text.getText(),"-"); double d1,d2 = 0; d1 = Double.parseDouble(str1.nextToken()); while(str1.hasMoreTokens()){ StringTokenizer str2 = new StringTokenizer(str1.nextToken(),"="); d2 = Double.parseDouble(str2.nextToken()); } text.setText(text.getText() + " " + (d1-d2)); }else if(text.getText().contains("*")) { StringTokenizer str1 = new StringTokenizer(text.getText(),"*"); double d1,d2 = 0; d1 = Double.parseDouble(str1.nextToken()); while(str1.hasMoreTokens()){ StringTokenizer str2 = new StringTokenizer(str1.nextToken(),"="); d2 = Double.parseDouble(str2.nextToken()); } text.setText(text.getText() + " " + (d1*d2)); }else if (text.getText().contains("/")){ StringTokenizer str1 = new StringTokenizer(text.getText(),"/"); double d1,d2 = 0; d1 = Double.parseDouble(str1.nextToken()); while(str1.hasMoreTokens()){ StringTokenizer str2 = new StringTokenizer(str1.nextToken(),"="); d2 = Double.parseDouble(str2.nextToken()); } text.setText(text.getText() + " " + (d1/d2)); } } }); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { text.setText(""); } }); } private void Text(final JButton button) {//Event handling method of each button button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String str = button.getText(); text.setText(text.getText() + str); } }); } private void Panel() { panel = new JPanel(); //New calculator number and symbol area panel panel.setLayout(new GridLayout(4,4)); //Set panel layout //Place the button components in the panel //first line panel.add(jb7); panel.add(jb8); panel.add(jb9); panel.add(div); //Second elements panel.add(jb4); panel.add(jb5); panel.add(jb6); panel.add(mul); //Third elements panel.add(jb1); panel.add(jb2); panel.add(jb3); panel.add(sub); //Fourth elements panel.add(bot); panel.add(jb0); panel.add(equ); panel.add(add); //panel.add(clear); this.add(panel,BorderLayout.CENTER); Panel sourth = new Panel(); sourth.add(clear); this.add(sourth,BorderLayout.SOUTH);//Place the Panel to the south of the Frame } private void init() { this.setBounds(800, 300, 400, 400); //Form size this.setTitle("WhitestăŽCabbage - simple calculator"); //Form name this.setLayout(new BorderLayout()); text = new JTextField(30); //Computation area this.add(text,BorderLayout.NORTH); //New number button jb0 = new JButton("0"); jb1 = new JButton("1"); jb2 = new JButton("2"); jb3 = new JButton("3"); jb4 = new JButton("4"); jb5 = new JButton("5"); jb6 = new JButton("6"); jb7 = new JButton("7"); jb8 = new JButton("8"); jb9 = new JButton("9"); //New symbol button bot = new JButton("."); add = new JButton("+"); sub = new JButton("-"); mul = new JButton("*"); div = new JButton("/"); equ = new JButton("="); clear = new JButton("C"); this.setIconImage(Toolkit.getDefaultToolkit().createImage("Calculator.png")); this.setVisible(true); //Set form display } public static void main(String[] args) { new Demo1_topic01(); } }
Operation interface