目录
编译代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
public class Calculator implements ActionListener{
JTextArea jf1;
public static Stack<String> stack1 = new Stack<>();
public static Stack<String> stack2 = new Stack<>();
public Calculator(){
JFrame frame=new JFrame("计算器");
frame.getContentPane().setBackground(Color.white);
frame.setSize(435,600);
frame.setLayout(null);
Font font=new Font("仿宋",Font.BOLD,20);
jf1 = new JTextArea("");
jf1.setBounds(10,10,400,165);
jf1.setFont(font);
JButton jb1=new JButton("1");
jb1.setBounds(10,250,100,75);
jb1.setFont(font);jb1.setBackground(Color.white);
jb1.setActionCommand("1");//设置按钮标识符
jb1.addActionListener(this);//添加对"1"的监听,传入对象信息
JButton jb2=new JButton("2");
jb2.setBounds(110,250,100,75);
jb2.setFont(font);jb2.setBackground(Color.white);
jb2.setActionCommand("2");//设置按钮标识符
jb2.addActionListener(this);//添加对"1"的监听,传入对象信息
JButton jb3=new JButton("3");
jb3.setBounds(210,250,100,75);
jb3.setFont(font);jb3.setBackground(Color.white);
jb3.setActionCommand("3");//设置按钮标识符
jb3.addActionListener(this);//添加对"1"的监听,传入对象信息
JButton jb4=new JButton("+");
jb4.setBounds(310,175,100,75);
jb4.setFont(font);jb4.setBackground(Color.white);
jb4.setActionCommand("+");//设置按钮标识符
jb4.addActionListener(this);//添加对"1"的监听,传入对象信息
JButton jb16=new JButton("*");
jb16.setBounds(310,325,100,75);
jb16.setFont(font);jb16.setBackground(Color.white);
jb16.setActionCommand("*");//设置按钮标识符
jb16.addActionListener(this);//添加对"1"的监听,传入对象信息
JButton jb5=new JButton("4");
jb5.setBounds(10,325,100,75);