This article shares the functions of Java implementing simple calculator. The specific content is as follows
topic:
Write a program that simulates a calculator. Add a text box to the panel (displaying keys and calculation results),
10 numeric buttons (0~9), 4 operation buttons (add, subtract, multiply, divide), an equal sign button, and a clear button.
Requires the keys and results to be displayed in the text box.
Code process display:
import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class Exercise1 extends JFrame implements ActionListener{ private JPanel p1 = new JPanel(); //Create panel private JPanel p2 = new JPanel(); //Create panel private JTextField t1; //Text box 1 is used to display input information StringBuffer str; //Input string JButton[] b=new JButton[10]; JButton b1,b2,b3,b4,b5,b6; //16 buttons double x,y; int n; public Exercise1() { super("Fake Captain's big goal"); setSize(350,300); //Set the window size setLocationRelativeTo(null); //Show to the middle Container c = getContentPane(); //Create the content panel object t1 = new JTextField(25); t1.setEditable(false); //Only display, p2.add(t1); //Add text boxes to the panel p2.setLayout(new GridLayout(3,2)); //Layout the face to 4 rows and 1 column str=new StringBuffer(); //Instance each button for(int i=0;i<10;i++) //Set labels for buttons 0~9 in the array, and register the listener { String s=""+i; b[i]= new JButton(s); b[i].addActionListener(this); } b1=new JButton("+"); b2=new JButton("-"); b3=new JButton("*"); b4=new JButton("/"); b5=new JButton("="); b6=new JButton("delete"); //Add to panel p1.add(b[7]); p1.add(b[8]); p1.add(b[9]); p1.add(b1); p1.add(b[4]); p1.add(b[5]); p1.add(b[6]); p1.add(b2); p1.add(b[1]); p1.add(b[2]); p1.add(b[3]); p1.add(b3); p1.add(b[0]); p1.add(b5); p1.add(b6); p1.add(b4); p1.setLayout(new GridLayout(4,5,10,10)); //Register the listener b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); //Add the content to the panel and add to the container c.add(p2); c.add(p1); c.setLayout(new FlowLayout()); //Set to sequential layout//Set window close action setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set window close action setVisible(true); //Set to visible setResizable(false); //Frame size is prohibited} public static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("unused") Exercise1 calculate=new Exercise1(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==b6){ t1.setText("0"); //Clear t1.setHorizontalAlignment(JTextField.RIGHT); //right-aligning str.setLength(0); } //Double.parseDouble Converts the string to double type //t1.getText().trim() Get the character saved and clears else if (e.getSource()==b1)//Click the plus button to get the value of x and clear the value of y{ x=Double.parseDouble(t1.getText().trim()); str.setLength(0); y=0d; n=0; }else if(e.getSource()==b2)//Subtraction operation { x=Double.parseDouble(t1.getText().trim()); str.setLength(0); y=0d; n=1; }else if(e.getSource()==b3)//Multiple operation { x=Double.parseDouble(t1.getText().trim()); str.setLength(0); y=0d; n=2; }else if(e.getSource()==b4)//Divide operation { x=Double.parseDouble(t1.getText().trim()); str.setLength(0); y=0d; n=3; }else if(e.getSource()==b5)//Equal sign { str.setLength(0); switch(n){ case 0:t1.setText(""+(x+y));break; case 1:t1.setText(""+(xy));break; case 2:t1.setText(""+(x*y));break; case 3:t1.setText(""+(x/y));break; } }else{ if(e.getSource()==b[0]) { if(t1.getText().trim().equals("0"))//If the display is zero, do not operate {} else t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT); y=Double.parseDouble(t1.getText().trim()); } else { t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT); y=Double.parseDouble(t1.getText().trim()); } } } }Summary: The code is a bit lengthy, but it is not complicated after real understanding. Of course, this is just a simple simulation calculator.
Other features can also be added to it. For example, adding exponential operations, exponent operations, square operations, or to make the interface beautiful,
Add a result text box, with the entered number displayed above and the result displayed below. Of course, to say so much, it still depends on readers to study it themselves.
All the above are the entire content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.