实现简单的整数加减乘除四则运算
###############################################
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
//定义所有需要使用的全局变量
public String str="";//暂存显示屏数据
public String sign="null";//暂存符号数据
double num=0;//暂存内部运算数据
boolean change=false;//暂存内部四则运算起用
/**入口主函数
* @param args
*/
public static void main(String[] args) {
//实例化一个计算器类对象
Calculator ca=new Calculator();
//调用类内部定义的initUI()方法初始化界面
ca.initUI();
}
/** 界面初始化函数
* @param args
*/
public void initUI(){
//创建一个窗体对象
JFrame jf=new JFrame();
//设置窗体属性值
jf.setTitle("计算器");
jf.setSize(300,400);
jf.setLocation(200, 200);
jf.setDefaultCloseOperation(3);
jf.setResizable(false);
//设置背景色
//jf.setBackground(new Color(65,143,211));
//设置背景图片
ImageIcon image = new ImageIcon("images/calculator.jpg");
JLabel background = new JLabel(image);
background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
//装有背景图片的标签添加到第二层面板上
jf.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
//将上面一层面板设为透明
JPanel contentPanel=(JPanel)jf.getContentPane();
contentPanel.setOpaque(false);
//给窗体添加一个BorderLayout管理器对象
java.awt.BorderLayout bl=new java.awt.BorderLayout();
jf.setLayout(bl);
bl.setHgap(5);//设置组件之间的水平间距
bl.setVgap(10);//设置组件之间的垂直间距
//在窗体的北部嵌套一个面板容器
JPanel northPanel=new JPanel();
northPanel.setPreferredSize(new Dimension(0,100));
northPanel.setOpaque(false);
JLabel jlb = new JLabel("计算器");
jlb.setFont(new Font("黑体",1,15));
northPanel.add(jlb );
JTextField jtx =new JTextField(17);
jtx.setPreferredSize(new Dimension(0,60));
jtx.setFont(new Font("",1,16));
northPanel.add(jtx);
jf.add(northPanel,BorderLayout.NORTH);
//给东、西、南部分别设置面板留白(设置透明后添加)
JPanel westPanel =new JPanel();
westPanel.setPreferredSize(new Dimension(20,0));
westPanel.setOpaque(false);
JPanel eastPanel =new JPanel();
eastPanel.setPreferredSize(new Dimension(20,0));
eastPanel.setOpaque(false);
JPanel southPanel =new JPanel();
southPanel.setPreferredSize(new Dimension(0,20));
southPanel.setOpaque(false);
jf.add(westPanel,BorderLayout.WEST);
jf.add(eastPanel,BorderLayout.EAST);
jf.add(southPanel,BorderLayout.SOUTH);
//创建中部面板
JPanel centerPanel=setCenterPanel(jtx);
//将中部面板添加到窗体中
jf.add(centerPanel,BorderLayout.CENTER);
//最后设置显示窗体
jf.setVisible(true);
}
/** 中部面板设置函数
* @return JPanel
*/
public JPanel setCenterPanel(JTextField jtx) {
JPanel jpa2 =new JPanel();
//设置透明色
jpa2.setOpaque(false);
//给jpa2添加一个4行5列、列距和行距分别为10和5的边框布局管理器对象
jpa2.setLayout(new GridLayout(5,4,10,5));
//依次创建并添加各个按钮对象
JButton jbmc =new JButton("cl");
jpa2.add(jbmc);
JButton jbmr =new JButton("mr");
jpa2.add(jbmr);
JButton jbms =new JButton("ms");
jpa2.add(jbms);
JButton jb11 =new JButton("←");
jpa2.add(jb11);
JButton jb7 =new JButton("7");
jb7.setFont(new Font("",1,16));
jb7.setBackground(new Color(162,171,255));
jb7.setForeground(Color.WHITE);
jpa2.add(jb7);
JButton jb8 =new JButton("8");
jb8.setFont(new Font("",1,16));
jb8.setBackground(new Color(162,171,255));
jb8.setForeground(Color.WHITE);
jpa2.add(jb8);
JButton jb9 =new JButton("9");
jb9.setFont(new Font("",1,16));
jb9.setBackground(new Color(162,171,255));
jb9.setForeground(Color.WHITE);
jpa2.add(jb9);
JButton jbpl =new JButton("+");
jpa2.add(jbpl);
JButton jb4 =new JButton("4");
jb4.setFont(new Font("",1,16));
jb4.setBackground(new Color(162,171,255));
jb4.setForeground(Color.WHITE);
jpa2.add(jb4);
JButton jb5 =new JButton("5");
jb5.setFont(new Font("",1,16));
jb5.setBackground(new Color(162,171,255));
jb5.setForeground(Color.WHITE);
jpa2.add(jb5);
JButton jb6 =new JButton("6");
jb6.setFont(new Font("",1,16));
jb6.setBackground(new Color(162,171,255));
jb6.setForeground(Color.WHITE);
jpa2.add(jb6);
javax.swing.JButton jbmi =new JButton("—");
jpa2.add(jbmi);
JButton jb1 =new JButton("1");
jb1.setFont(new Font("",1,16));
jb1.setBackground(new Color(162,171,255));
jb1.setForeground(Color.WHITE);
jpa2.add(jb1);
JButton jb2 =new JButton("2");
jb2.setFont(new Font("",1,16));
jb2.setBackground(new Color(162,171,255));
jb2.setForeground(Color.WHITE);
jpa2.add(jb2);
JButton jb3 =new JButton("3");
jb3.setFont(new Font("",1,16));
jb3.setBackground(new Color(162,171,255));
jb3.setForeground(Color.WHITE);
jpa2.add(jb3);
JButton jbmu =new JButton("×");
jpa2.add(jbmu);
JButton jb0 =new JButton("0");
jpa2.add(jb0);
JButton jbpo =new JButton(". ");
jpa2.add(jbpo);
JButton jbis =new JButton("=");
jpa2.add(jbis);
JButton jbdi =new JButton("÷");
jpa2.add(jbdi);
Calculator calculator=new Calculator();
//实例化各个事件处理类对象(参数略多)
//给各个事件源添加监听方法
InputListener il=new InputListener(calculator,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb0,jbpl,jbmi,jbmu,jbdi,jbis,jb11,jbmc,jtx);
jb1.addActionListener(il);
jb2.addActionListener(il);
jb3.addActionListener(il);
jb4.addActionListener(il);
jb5.addActionListener(il);
jb6.addActionListener(il);
jb7.addActionListener(il);
jb8.addActionListener(il);
jb9.addActionListener(il);
jb0.addActionListener(il);
jbpl.addActionListener(il);
jbmi.addActionListener(il);
jbmu.addActionListener(il);
jbdi.addActionListener(il);
jbis.addActionListener(il);
jb11.addActionListener(il);
jbmc.addActionListener(il);
return jpa2;
}
}
#######################事件处理类定义########################
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JTextField;
public class InputListener implements ActionListener {
//所有要涉及到的对象设置为此类的属性
private Calculator ca;
private JTextField jtx;
private JButton jb1;
private JButton jb2;
private JButton jb3;
private JButton jb4;
private JButton jb5;
private JButton jb6;
private JButton jb7;
private JButton jb8;
private JButton jb9;
private JButton jb0;
private JButton jbpl;
private JButton jbmi;
private JButton jbmu;
private JButton jbdi;
private JButton jbis;
private JButton jb11;
private JButton jbmc;
//利用构造函数传参(所有的按钮对象、文本框对象以及一个Calculator类对象)
public InputListener (Calculator ca,JButton jb1,JButton jb2, JButton jb3,JButton jb4,JButton jb5, JButton jb6,JButton jb7,JButton jb8, JButton jb9,JButton jb0,JButton jbpl,JButton jbmi,JButton jbmu,JButton jbdi,JButton jbis,JButton jb11,JButton jbmc,JTextField jtx) {
this.ca=ca;
this.jtx=jtx;
this.jb1=jb1;
this.jb2=jb2;
this.jb3=jb3;
this.jb4=jb4;
this.jb5=jb5;
this.jb6=jb6;
this.jb7=jb7;
this.jb8=jb8;
this.jb9=jb9;
this.jb0=jb0;
this.jbpl=jbpl;
this.jbmi=jbmi;
this.jbmu=jbmu;
this.jbdi=jbdi;
this.jbis=jbis;
this.jb11=jb11;
this.jbmc=jbmc;
}
public void actionPerformed(ActionEvent e) {
Object currKey = e.getSource();//Object为所有类的父类,创建一个Object类对象表示当前监听到的操作
//列出所有情况(所有的button分别被按下的情况)
if (currKey == jb1) { //对1做出反应
pressjb1();
} else if (currKey == jb2) { //对2做出反应
pressjb2();
} else if (currKey == jb3) { //以此类推,以下略
pressjb3();
} else if (currKey == jb4) {
pressjb4();
} else if (currKey == jb5) {
pressjb5();
} else if (currKey == jb6) {
pressjb6();
} else if (currKey == jb7) {
pressjb7();
} else if (currKey == jb8) {
pressjb8();
} else if (currKey == jb9) {
pressjb9();
} else if (currKey == jb0) {
pressjb0();
} else if(e.getSource()==jbpl) {//对+做出反应
ca.sign="add";
ca.num=Double.parseDouble(ca.str);
ca.change=true;//起标记作用,表明已经输入一个运算符了
} else if(e.getSource()==jbmi) {//对-做出反应
ca.sign="sub";
ca.num=Double.parseDouble(ca.str);
ca.change=true;
} else if(e.getSource()==jbmu) {//对*做出反应
ca.sign="mul";
ca.num=Double.parseDouble(ca.str);
ca.change=true;
} else if(e.getSource()==jbdi) {//对/做出反应
ca.sign="div";
ca.num=Double.parseDouble(ca.str);
ca.change=true;
} else if(e.getSource()==jbis) {//对按=作出反应
if(ca.sign=="null")//1,没有输入运算符(仅有数据)
jtx.setText(ca.str);//则直接输出(数据)
else if(ca.sign.equals("add"))//2,输入运算符
{
ca.num+=Double.parseDouble(ca.str);//将文本框中的字符串转为double数据类型并
ca.str=Double.toString(ca.num);//重新设置文本框的内容以字符串形式输出
jtx.setText(ca.str);
}
else if(ca.sign.equals("sub"))
{
ca.num-=Double.parseDouble(ca.str);
ca.str=Double.toString(ca.num);
jtx.setText(ca.str);
}
else if(ca.sign.equals("mul"))
{
ca.num*=Double.parseDouble(ca.str);
ca.str=Double.toString(ca.num);
jtx.setText(ca.str);
} else if(ca.sign.equals("div")) {
if(ca.num!=0) {//判断除数非零
ca.num/=Double.parseDouble(ca.str);
ca.str=Double.toString(ca.num);
jtx.setText(ca.str);
}
else {
ca.str="0";
jtx.setText(ca.str);
}
}
ca.sign="null";
} else if (e.getSource() == jb11) {
pressjb11();
} else if (e.getSource() == jbmc){
pressjbmc();
}
}
public void pressjb1 () {//对按键1作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="1";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="1";
ca.change=false;//将change置为fasle以便后面还能输入字符
}
jtx.setText(ca.str);
}
public void pressjb2 () {//对按键2作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="2";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="2";
ca.change=false;//将change置为fasle以便后面还能输入字符
}
jtx.setText(ca.str);
}
public void pressjb3 () {//对按键3作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="3";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="3";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb4 () {//对按键4作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="4";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="4";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb5 () {//对按键5作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="5";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="5";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb6 () {//对按键6作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="6";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="6";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb7 () {//对按键7作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="7";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="7";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb8 () {//对按键8作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="8";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="8";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb9 () {//对按键9作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="9";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="9";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb0 () {//对按键0作出反应的方法
if(ca.str.length()<16 && ca.str!="" && ca.change==false)
{
ca.str+="0";
}
else if(ca.str=="" || ca.sign!="null")
{
ca.str="0";
ca.change=false;
}
jtx.setText(ca.str);
}
public void pressjb11 () {//对按下←作出反应的方法
if(ca.str.length()>1)
{
ca.str=ca.str.substring(0,ca.str.length()-1);
jtx.setText(ca.str);
}
else if(ca.str.length()==1)
{
ca.str="0";
jtx.setText(ca.str);
}
}
public void pressjbmc() {//对清零按键做出反应的方法
//将所有变量置为初始值
jtx.setText(null);
ca.str="";
ca.sign="null";
ca.num=0;
ca.change=false;
}
}