package show;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class l0 extends JFrame implements ActionListener {
public double sum=0;
private JTextField text1=new JTextField("0.0",22);
JPanel pa = new JPanel(new GridLayout(4, 3, 5, 5));
public l0(){
super("计算器");
this.setSize(400, 500);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text1.setForeground(Color.black);
text1.setFont(new Font("微软雅黑", Font.PLAIN, 50));
getContentPane().add("North", text1);
pa.setBackground(Color.white);
getContentPane().add("Center", pa);
String str0[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3"};
String str1[] = {"-", "+", "="};
JButton[] bu = new JButton[9] ;
JButton[] tu = new JButton[3] ;
for (int i = 0; i < str0.length; i++) {
bu[i]= new JButton(str0[i]);
pa.add(bu[i]);
bu[i].setForeground(Color.yellow);
bu[i].setBackground(Color.gray);
bu[i].setFont(new Font("宋体", Font.BOLD, 30));
}
for (int i = 0; i < str1.length; i++) {
tu[i]= new JButton(str1[i]);
pa.add(tu[i]);
tu[i].setForeground(Color.yellow);
tu[i].setBackground(Color.gray);
tu[i].setFont(new Font("宋体", Font.BOLD, 30));
}
//监听事件 监听整个面板
ButtonListener button = new ButtonListener() ;
for(int i = 0; i < str0.length; i++)
bu[i].addActionListener(button) ;
for(int i=0;i<str1.length;i++)
tu[i].addActionListener(button) ;
}
class ButtonListener implements ActionListener {
private String strVal; //存储数字对应的字符串
private String number; //存储新输入的数
private boolean firsttime; //判断是否第一次按下的是操作符按钮
private boolean operatorPressed=true;//判断是否已经按过操作符按钮
private String res;
private int choose=0;
@Override
//判断按的是数字还是操作符
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str = ((JButton)e.getSource()).getText().trim() ;
if(Character.isDigit(str.charAt(0)))
handleNumber(str) ;
else
handleUnaryOp(str) ;
}
//处理一元操作符
public void handleUnaryOp(String op)
{
if(op.equals("+"))
{
number = text1.getText();
System.out.println(number);
text1.setText("");
text1.setText(number + "+");
choose=1;
return;
}else if(op.equals("="))
{
firsttime = true;
text1.setText(res);
choose=3;
return;
}else if(op.equals("-"))
{
number = text1.getText();
System.out.println(number);
text1.setText("");
text1.setText(number + "-");
choose=2;
return;
}
else
{
clear();
}
}
//该方法用于处理数字按钮
public void handleNumber(String s)
{
if(operatorPressed==false)
{
if( choose==3)
{
clear();
return;
}
if( choose==1)
{
int b=Integer.parseInt(strVal);
int a = Integer.parseInt(s);
res=a+b+"";
strVal = strVal+"+"+s;
}
if( choose==2)
{
int b=Integer.parseInt(strVal);
int a = Integer.parseInt(s);
res=b-a+"";
strVal = strVal+"-"+s;
}
}
else
{
operatorPressed = false;
strVal = s;
}
System.out.println(operatorPressed);
// number = new Double(strVal).doubleValue();
text1.setText("");
text1.setText(strVal);
System.out.println(strVal);
}
//清除屏幕并设置所有的标识
public void clear()
{
firsttime = true;
strVal = "";
number = "";
text1.setText("0");
}
}
public static void main(String[] args) {
l0 my1=new l0();
my1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
参考:https://13485712774-139-com.iteye.com/blog/1055840