学习java时写的一个基于windows的简单计算器
BuildBtn.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.JButton;
/**
* 构造有监听器和颜色的按钮
*/
public class BuildBtn extends JButton {
private String btnName;
private ActionListener listener;
private Color color;
public BuildBtn()
{
}
public BuildBtn(String btnName, ActionListener listener, Color color) {
super();
this.setFont(new Font("宋体",0,12));
this.setMargin(new Insets(0,0,0,0));
this.setText(btnName);
this.addActionListener(listener);
this.setForeground(color);
}
public String getBtnName() {
return btnName;
}
public void setBtnName(String btnName) {
this.btnName = btnName;
}
public ActionListener getListener() {
return listener;
}
public void setListener(ActionListener listener) {
this.listener = listener;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
Calculator.java
package com.demo;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.sun.java.swing.plaf.windows.*;
/**
* 计算器面板布局
*/
public class Calculator extends JFrame {
private JButton b[];
private JButton dotbtn,addbtn,subbtn,mulbtn,divbtn,backspace,
c,equalbtn,ce,mc,mr,ms,mm,dif,sqrt,percent,rec;
private GridLayout gl1,gl2,gl3;
private JTextField txtinput,mText;
private JPanel p0,p1,p2,p3;
private String showStr = "";//显示条显示的数字
private Integer flag = 0; //标志位
private Double operatorX = 0d,operatorY = 0d; //操作数
private Double result; //操作结果
private Boolean boperator = false;//判断是否是在点击运算符
private Boolean bre = false; //是否第一次按下操作数
private Integer nIndex = 0; //记录当前的操作数个数
private static Double m = 0d; //记忆数字
private Boolean isValue = false;//第二个操作数是否有值
private Boolean isoperator = false;
public Calculator() throws HeadlessException {
// try {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// } catch (ClassNotFoundException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } catch (InstantiationException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } catch (IllegalAccessException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } catch (UnsupportedLookAndFeelException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
//设置Windows风格
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BtnListener btnListener = new BtnListener();
b = new BuildBtn[10];
for(int i = 0;i < 10;i++)
{
b[i] = new BuildBtn("" + i + "",btnListener,Color.blue);
}
// 声明三个面板的布局
gl1 = new GridLayout(1,3,5,0);
gl2 = new GridLayout(5,1,0,5);
gl3 = new GridLayout(4,5,5,5);
p0 = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
// 显示条
txtinput = new JTextField(36);
txtinput.setEditable(false);
txtinput.setBackground(Color.white);
txtinput.setHorizontalAlignment(JTextField.RIGHT);
txtinput.setText("0.");
p0.add(txtinput);
p0.setBounds(0, 20, 248, 25);
this.add(p0);
// 实例化按钮并添加面板p2中的组件和设置其在框架中的位置和大小
this.setLayout(null);
mText = new JTextField();
mText.setEditable(false);
mText.setBorder(BorderFactory.createLoweredBevelBorder());
mc = new BuildBtn("MC",btnListener,Color.red);
mr = new BuildBtn("MR",btnListener,Color.red);
ms = new BuildBtn("MS",btnListener,Color.red);
mm = new BuildBtn("M+",btnListener,Color.red);
p2.setLayout(gl2);
p2.add(mText);
p2.add(mc);
p2.add(mr);
p2.add(ms);
p2.add(mm);
//p2.setBackground(Color.red);
p2.setBounds(10, 55, 30, 150);
this.add(p2);
// 实例化按钮并添加面板p1中的组件和设置其在框架中的位置和大小
backspace = new BuildBtn("Backspace",btnListener,Color.red);
ce = new BuildBtn("CE",btnListener,Color.red);
c = new BuildBtn("C",btnListener,Color.red);
p1.setLayout(gl1);
//p1.setBackground(Color.red);
p1.add(backspace);
p1.add(ce);
p1.add(c);
p1.setBounds(48, 55, 190, 25);
this.add(p1);
// 实例化按钮并添加面板p3中的组件和设置其在框架中的位置和大小
dotbtn = new BuildBtn(".",btnListener,Color.blue);
addbtn = new BuildBtn("+",btnListener,Color.red);
subbtn = new BuildBtn("-",btnListener,Color.red);
mulbtn = new BuildBtn("*",btnListener,Color.red);
divbtn = new BuildBtn("/",btnListener,Color.red);
dif = new BuildBtn("+/-",btnListener,Color.blue);
sqrt = new BuildBtn("sqrt",btnListener,Color.blue);
percent = new BuildBtn("%",btnListener,Color.blue);
rec = new BuildBtn("1/x",btnListener,Color.blue);
equalbtn = new BuildBtn("=",btnListener,Color.red);
p3.setLayout(gl3);
//p3.setBackground(Color.green);
p3.add(b[7]);
p3.add(b[8]);
p3.add(b[9]);
p3.add(divbtn);
p3.add(sqrt);
p3.add(b[4]);
p3.add(b[5]);
p3.add(b[6]);
p3.add(mulbtn);
p3.add(percent);
p3.add(b[1]);
p3.add(b[2]);
p3.add(b[3]);
p3.add(subbtn);
p3.add(rec);
p3.add(b[0]);
p3.add(dif);
p3.add(dotbtn);
p3.add(addbtn);
p3.add(equalbtn);
p3.setBounds(48, 85, 190, 120);
this.add(p3);
// 关闭窗口
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setTitle("计算器");
this.setResizable(false);
this.setBackground(Color.darkGray);
this.setBounds(50, 50, 255, 245);
this.setVisible(true);
}
public class BtnListener implements ActionListener {
public void init()
{
showStr = "";
operatorX = 0d;
operatorY = 0d;
flag = 0;
result = 0d;
nIndex = 0;
isValue = false;
bre = false;
txtinput.setText("0.");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == ce)// 选择"CE"清零
{
this.init();
}else if(e.getSource() == c)// 选择"C"清零
{
this.init();
}else if(e.getSource() == backspace)//退格
{
if(txtinput.getText().equals("0."));
else if(txtinput.getText().length() == 1)
{
showStr = "";
txtinput.setText("0.");
}
else if(!boperator)
{
showStr = showStr.substring(0, showStr.length() - 1);
txtinput.setText(showStr);
//txtinput.setText(txtinput.getText().substring(0, txtinput.getText().length() - 1));
}
}else if(e.getSource() == mc)//清除记忆数字
{
bre = false;
showStr = "";
m = 0d;
mText.setText("");
}else if(e.getSource() == mr)//提起记忆数字
{
showStr = "";
if(m == 0)
txtinput.setText("0.");
else
{
txtinput.setText(m.toString());
if(flag == 0)
{
nIndex++;
if(nIndex > 2)nIndex = 2;
operatorX = m;
}
else
{
nIndex++;
if(nIndex > 2)nIndex = 2;
isValue = true;
operatorY = m;
}
}
}else if(e.getSource() == ms)//存取记忆数字
{
if(Double.parseDouble(txtinput.getText()) == 0);
else
{
showStr = "";
m = Double.parseDouble(txtinput.getText());
mText.setText("M");
}
}else if(e.getSource() == mm)//当前数字加上存取的记忆数作为新记忆数
{
showStr = "";
m += Double.parseDouble(txtinput.getText());
}else if(e.getSource() == b[0])//当输入0的时候
{
if(showStr.length() == 0);
else
{
showStr = txtinput.getText() + e.getActionCommand().toString();
txtinput.setText(showStr);
if(nIndex == 1)
operatorX = Double.parseDouble(showStr);
if(nIndex == 2)
operatorY = Double.parseDouble(showStr);
}
}else if(e.getSource() == dotbtn)//点击小数点的时候
{
if(showStr.contains("."));
else
{
if(bre)nIndex++;
if(showStr.length() == 0)
showStr += "0";
showStr += e.getActionCommand().toString();
txtinput.setText(showStr);
if(nIndex == 1)
operatorX = Double.parseDouble(showStr);
if(nIndex == 2)
operatorY = Double.parseDouble(showStr);
}
}else if(e.getSource() == dif)//改变符号
{
if(txtinput.getText().equals("0."));
else
{
Double temp = Double.parseDouble(txtinput.getText());
temp = -temp;
showStr = temp.toString();
txtinput.setText(showStr);
if(nIndex == 1)
operatorX = Double.parseDouble(showStr);
if(nIndex == 2)
operatorY = Double.parseDouble(showStr);
}
}else if(e.getSource() == addbtn)//点击加号
{
operatorNumber(bre,nIndex,flag);
bre = true;
flag = 1;
}else if(e.getSource() == subbtn)//点击减号
{
operatorNumber(bre,nIndex,flag);
bre = true;
flag = 2;
}else if(e.getSource() == mulbtn)//点击乘号
{
operatorNumber(bre,nIndex,flag);
bre = true;
flag = 3;
}else if(e.getSource() == divbtn)//点击除号
{
operatorNumber(bre,nIndex,flag);
bre = true;
flag = 4;
}else if(e.getSource() == sqrt)//开根号
{
boperator = true;
flag = 0;
if(Double.parseDouble(txtinput.getText()) < 0)
txtinput.setText("函数输入无效.");
else if(nIndex == 1)
{
operatorX = Math.sqrt(operatorX);
showStr = operatorX.toString();
txtinput.setText(showStr);
showStr = "";
}
else if(nIndex == 2)
{
operatorY = Math.sqrt(operatorY);
showStr = operatorY.toString();
txtinput.setText(showStr);
showStr = "";
}
}else if(e.getSource() == percent)//点击%号
{
boperator = true;
flag = 0;
if(nIndex < 2 && flag == 0)
{
this.init();
}
else
{
operatorX = operatorX*operatorY/100.0;
showStr = operatorX.toString();
txtinput.setText(showStr);
showStr = "";
}
}
else if(e.getSource() == rec)//点击求倒数
{
boperator = true;
flag = 0;
if(Double.parseDouble(txtinput.getText()) == 0)
txtinput.setText("除数不能为0.");
if(nIndex == 1)
{
operatorX = 1.0/operatorX;;
showStr = operatorX.toString();
txtinput.setText(showStr);
showStr = "";
}
if(nIndex == 2)
{
operatorY = 1.0/operatorY;;
showStr = operatorY.toString();
txtinput.setText(showStr);
showStr = "";
}
}else if(e.getSource() == equalbtn)//点击等号
{
operatorEqual(bre,nIndex,flag);
}
else
{
boperator = false;
showStr += e.getActionCommand().toString();
txtinput.setText(showStr);
if(bre&&flag != 0)
{
if(!isoperator)
{
operatorY = Double.parseDouble(txtinput.getText().trim());
isValue = true;
nIndex++;
if(nIndex > 2)nIndex = 2;
}
}
else//初始化第一个操作数
{
operatorX = Double.parseDouble(txtinput.getText().trim());
operatorY = Double.parseDouble(txtinput.getText().trim()); //默认两个操作数是一样的
nIndex++;
if(nIndex > 2)nIndex = 2;
}
}
}
}
/**
* 加减乘除运算
* @param bre 是否第一次按下操作数
* @param nIndex 操作数个数
* @param flag 标志位
*/
public void operatorNumber(Boolean bre,Integer nIndex,Integer flag)
{
isoperator = false;
boperator = true;
if(bre&&nIndex == 2)
{
switch(flag)
{
case 1:
operatorX += operatorY;
showStr = operatorX.toString();
txtinput.setText(showStr);
break;
case 2:
operatorX -= operatorY;
showStr = operatorX.toString();
txtinput.setText(showStr);
break;
case 3:
operatorX *= operatorY;
showStr = operatorX.toString();
txtinput.setText(showStr);
break;
case 4:
if(operatorY == 0)
txtinput.setText("除数不能为0.");
else
{
operatorX /= operatorY;
showStr = operatorX.toString();
txtinput.setText(showStr);
}
break;
}
}
result = operatorX;
showStr = "";
this.nIndex = 1;
}
/**
* =号运算
* @param bre 是否第一次按下操作数
* @param nIndex 操作数个数
* @param flag 标志位
*/
public void operatorEqual(Boolean bre,Integer nIndex,Integer flag)
{
boperator = true;
if(bre&&nIndex == 1)//操作连加
{
if(isValue)operatorX = operatorY;
switch(flag)
{
case 1:
result += operatorX;
showStr = result.toString();;
txtinput.setText(showStr);
break;
case 2:
result -= operatorX;
showStr = result.toString();;
txtinput.setText(showStr);
break;
case 3:
result *= operatorX;
showStr = result.toString();;
txtinput.setText(showStr);
break;
case 4:
if(operatorY == 0)
txtinput.setText("除数不能为0.");
else
{
result /= operatorX;
showStr = result.toString();;
txtinput.setText(showStr);
}
break;
}
showStr = "";
this.nIndex = 1;
}
if(bre&&nIndex == 2)//普通情况的求等
{
switch(flag)
{
case 1:
result = operatorX + operatorY;
showStr = result.toString();
txtinput.setText(showStr);
break;
case 2:
result = operatorX - operatorY;
showStr = result.toString();;
txtinput.setText(showStr);
break;
case 3:
result = operatorX*operatorY;
showStr = result.toString();;
txtinput.setText(showStr);
break;
case 4:
if(operatorY == 0)
txtinput.setText("除数不能为0.");
else
{
result = operatorX/operatorY;
showStr = result.toString();;
txtinput.setText(showStr);
}
break;
}
//operatorX = operatorY;
showStr = "";
this.nIndex = 1;
}
isoperator = true;
showStr = "";
operatorX = result;
bre = true;
}
}
test.java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
new Calculator();
}
}