//导入需要用到的包
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
//主函数
class Example
{
public static void main(String args[])
{
MyCalculator mycalculator=new MyCalculator("我的计算器");
}
}
class MyCalculator extends JFrame implements ActionListener
{
//声明文本框
JTextField textanswer;
boolean first=true; //初始化状态
double d1,d2; //定义两个操作数
int operator=10,preoperator; //运算符标志,加减乘除的值分别是0,1,2,3
//声明菜单栏
JMenuBar menubar;
//声明面板
JPanel panel1,panel2,panel3,panel4;
//菜单命令声明
JMenu menuedit,menuview,menuhelp;
JMenuItem copyitem,pasteitem,standarditem,scientificitem,hitem,aitem;
//声明按钮
JButton buttonBk, buttonCe, buttonC;
JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button0;
JButton buttonvoid ,buttonMC, buttonMR, buttonMS, buttonMadd;
JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
buttonDiv, buttonMod;
JButton buttonSqrt, buttonDao, buttonEq;
//设置double型数据的输出精度
MyCalculator(String s)
{
//创建窗体,设置窗体属性
super(s);//该方法必须放在第一行
setVisible(true);
setSize(500,300);
setLocation(300,300);
setResizable(false);
//创建菜单栏选项
menubar=new JMenuBar();
menuedit=new JMenu("编辑");
menuview=new JMenu("浏览");
menuhelp=new JMenu("帮助");
copyitem=new JMenuItem("复制");
copyitem.addActionListener(this);
pasteitem=new JMenuItem("粘贴");
pasteitem.addActionListener(this);
standarditem=new JMenuItem("标准型");
scientificitem=new JMenuItem("科学型");
hitem=new JMenuItem("帮助主题");
aitem=new JMenuItem("关于计算器");
//创建按钮,并设置前景色
buttonvoid=new JButton(" ");
buttonvoid.setBackground(new Color(215, 218, 239));
buttonMC = new JButton("MC");
buttonMC.setForeground(new Color(255, 0, 0));
buttonMR = new JButton("MR");
buttonMR.setForeground(new Color(255, 0, 0));
buttonMS = new JButton("MS");
buttonMS.setForeground(new Color(255, 0, 0));
buttonMadd = new JButton("M+");
buttonMadd.setForeground(new Color(255, 0, 0));
buttonBk=new JButton("Backspace");
buttonBk.addActionListener(this);
buttonBk.setForeground(new Color(255, 0, 0));
buttonCe=new JButton("CE");
buttonCe.addActionListener(this);
buttonCe.setForeground(new Color(255, 0, 0));
buttonC=new JButton("C");
buttonC.addActionListener(this);
buttonC.setForeground(new Color(255, 0, 0));
buttonDot = new JButton(".");
buttonDot.addActionListener(this);
buttonDot.setForeground(new Color(255, 0, 0));
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
buttonAdd.setForeground(new Color(255, 0, 0));
buttonSub = new JButton("-");
buttonSub.addActionListener(this);
buttonSub.setForeground(new Color(255, 0, 0));
buttonMul = new JButton("*");
buttonMul.addActionListener(this);
buttonMul.setForeground(new Color(255, 0, 0));
buttonDiv = new JButton("/");
buttonDiv.addActionListener(this);
buttonDiv.setForeground(new Color(255, 0, 0));
buttonMod = new JButton("%");
buttonMod.addActionListener(this);
buttonMod.setForeground(new Color(0, 0, 255));
buttonSqrt = new JButton("sqrt");
buttonSqrt.addActionListener(this);
buttonSqrt.setForeground(new Color(0, 0, 255));
buttonDao = new JButton("1/x");
buttonDao.addActionListener(this);
buttonDao.setForeground(new Color(0, 0, 255));
buttonEq = new JButton("=");
buttonEq.addActionListener(this);
buttonEq.setForeground(new Color(255, 0, 0));
buttonAddAndSub =new JButton("+/-");
buttonAddAndSub.addActionListener(this);
buttonAddAndSub.setForeground(new Color(0, 0, 255));
button7=new JButton("7");
button7.addActionListener(this);
button8=new JButton("8");
button8.addActionListener(this);
button9=new JButton("9");
button9.addActionListener(this);
button4=new JButton("4");
button4.addActionListener(this);
button5=new JButton("5");
button5.addActionListener(this);
button6=new JButton("6");
button6.addActionListener(this);
button1=new JButton("1");
button1.addActionListener(this);
button2=new JButton("2");
button2.addActionListener(this);
button3=new JButton("3");
button3.addActionListener(this);
button0=new JButton("0");
button0.addActionListener(this);
//添加菜单栏
menuedit.add(copyitem);
menuedit.addSeparator();
menuedit.add(pasteitem);
menuview.add(standarditem);
menuview.addSeparator();
menuview.add(scientificitem);
menuhelp.add(hitem);
menuhelp.addSeparator();
menuhelp.add(aitem);
menubar.add(menuedit);
menubar.add(menuview);
menubar.add(menuhelp);
setJMenuBar(menubar);
//定义窗口的默认关闭方式
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//得到窗口面板
Container con=getContentPane();
//创建面板
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
//创建初始值为“0.”的文本框,设置可见性,文本对齐方式
textanswer=new JTextField("0.");
textanswer.setEditable(false);
textanswer.setHorizontalAlignment(JTextField.RIGHT);
//设置主面板的布局方式
con.setLayout(new BorderLayout(3,6));
//添加文本框
con.add(textanswer,BorderLayout.NORTH);
//设置面板1的布局方式,并添加按钮
panel1.setLayout(new GridLayout(5,1,0,6));
//在面板1中添加按钮,并把面板1添加到主面板中
panel1.add(buttonvoid);
panel1.add(buttonMC);
panel1.add(buttonMR);
panel1.add(buttonMS);
panel1.add(buttonMadd);
con.add(panel1,BorderLayout.WEST);
//设置面板2的布局方式
panel2.setLayout(new BorderLayout(0,17));
//设置面板3的布局方式,并添加按钮,把面板3添加到面板2中
panel3.setLayout(new GridLayout(1,3,3,3));
panel3.add(buttonBk);
panel3.add(buttonCe);
panel3.add(buttonC);
panel2.add(panel3,BorderLayout.NORTH);
//设置面板4的布局方式,并添加按钮
panel4.setLayout(new GridLayout(4,5,3,6));
panel4.add(button7);
panel4.add(button8);
panel4.add(button9);
panel4.add(buttonDiv);
panel4.add(buttonSqrt);
panel4.add(button4);
panel4.add(button5);
panel4.add(button6);
panel4.add(buttonMul);
panel4.add(buttonMod);
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
panel4.add(buttonSub);
panel4.add(buttonDao);
panel4.add(button0);
panel4.add(buttonAddAndSub);
panel4.add(buttonDot);
panel4.add(buttonAdd);
panel4.add(buttonEq);
panel2.add(panel4,BorderLayout.CENTER);
con.add(panel2,BorderLayout.CENTER);
validate();
// 设置double型数据的输出精度
}
public void actionPerformed(ActionEvent event)
{
Object temp = event.getSource();
if(event.getSource()==button0)
{
if(first)
{
textanswer.setText("0.");
}
else
{
String s=textanswer.getText();
s=s+"0";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button1)
{
if(first)
{
textanswer.setText("1");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"1";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button2)
{
if(first)
{
textanswer.setText("2");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"2";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button3)
{
if(first)
{
textanswer.setText("3");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"3";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button4)
{
if(first)
{
textanswer.setText("4");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"4";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button5)
{
if(first)
{
textanswer.setText("5");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"5";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button6)
{
if(first)
{
textanswer.setText("6");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"6";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button7)
{
if(first)
{
textanswer.setText("7");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"7";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button8)
{
if(first)
{
textanswer.setText("8");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"8";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==button9)
{
if(first)
{
textanswer.setText("9");
first=false;
}
else
{
String s=textanswer.getText();
s=s+"9";
textanswer.setText(s);
first=false;
}
}
else if(event.getSource()==buttonDot)
{
boolean hasDot=false;
for (int i = 0; i < textanswer.getText().length(); i++)
if ('.' == textanswer.getText().charAt(i))
{
hasDot = true;
break;
}
if (hasDot == false||textanswer.getText()=="0.")
textanswer.setText(textanswer.getText() + ".");
first=false;
hasDot=true;
}
else if(event.getSource()==buttonAdd)
{
String s=textanswer.getText();
if(operator==10)
{
d1=Double.parseDouble(s);
}
else
{
switch(preoperator)
{
case 0:
d2=Double.parseDouble(s);
d1=d1+d2;
textanswer.setText(""+d1);
break;
case 1:
d2=Double.parseDouble(s);
d1=d1-d2;
textanswer.setText(""+d1);
break;
case 2:
d2=Double.parseDouble(s);
d1=d1*d2;
textanswer.setText(""+d1);
break;
case 3:
d2=Double.parseDouble(s);
if(d2==0)
textanswer.setText("除数不能为0");
else
{
d1=d1/d2;
textanswer.setText(""+d1);
}
break;
}
}
first=true;
preoperator=operator=0;
}
else if(event.getSource()==buttonSub)
{
String s=textanswer.getText();
if(operator==10)
{
d1=Double.parseDouble(s);
}
else
{
switch(preoperator)
{
case 0:
d2=Double.parseDouble(s);
d1=d1+d2;
textanswer.setText(""+d1);
break;
case 1:
d2=Double.parseDouble(s);
d1=d1-d2;
textanswer.setText(""+d1);
break;
case 2:
d2=Double.parseDouble(s);
d1=d1*d2;
textanswer.setText(""+d1);
break;
case 3:
d2=Double.parseDouble(s);
if(d2==0)
textanswer.setText("除数不能为0");
else
{
d1=d1/d2;
textanswer.setText(""+d1);
}
break;
}
}
first=true;
preoperator=operator=1;
}
else if(event.getSource()==buttonMul)
{
String s=textanswer.getText();
if(operator==10)
{
d1=Double.parseDouble(s);
}
else
{
switch(preoperator)
{
case 0:
d2=Double.parseDouble(s);
d1=d1+d2;
textanswer.setText(""+d1);
break;
case 1:
d2=Double.parseDouble(s);
d1=d1-d2;
textanswer.setText(""+d1);
break;
case 2:
d2=Double.parseDouble(s);
d1=d1*d2;
textanswer.setText(""+d1);
break;
case 3:
d2=Double.parseDouble(s);
if(d2==0)
textanswer.setText("除数不能为0");
else
{
d1=d1/d2;
textanswer.setText(""+d1);
}
break;
}
}
first=true;
preoperator=operator=2;
}
else if(event.getSource()==buttonDiv)
{
String s=textanswer.getText();
if(operator==10)
{
d1=Double.parseDouble(s);
}
else
{
switch(preoperator)
{
case 0:
d2=Double.parseDouble(s);
d1=d1+d2;
textanswer.setText(""+d1);
break;
case 1:
d2=Double.parseDouble(s);
d1=d1-d2;
textanswer.setText(""+d1);
break;
case 2:
d2=Double.parseDouble(s);
d1=d1*d2;
textanswer.setText(""+d1);
break;
case 3:
d2=Double.parseDouble(s);
if(d2==0)
textanswer.setText("除数不能为0");
else
{
d1=d1/d2;
textanswer.setText(""+d1);
}
break;
}
}
first=true;
preoperator=operator=3;
}
else if(event.getSource()==buttonEq)
{
String s=textanswer.getText();
d2=Double.parseDouble(s);
switch(preoperator)
{
case 0:
d1=d1+d2;
textanswer.setText(""+d1);
break;
case 1:
d1=d1-d2;
textanswer.setText(""+d1);
break;
case 2:
d1=d1*d2;
textanswer.setText(""+d1);
break;
case 3:
if(d2==0)
textanswer.setText("除数不能为0");
else
{
d1=d1/d2;
textanswer.setText(""+d1);
}
break;
}
textanswer.setText(""+d1);
first=true;
operator=10;
}
else if(event.getSource()==buttonSqrt)
{
String s=textanswer.getText();
d1=Double.parseDouble(s);
d2=Math.sqrt(d1);
textanswer.setText(""+d2);
first=true;
}
else if(event.getSource()==buttonDao)
{
String s=textanswer.getText();
d1=Double.parseDouble(s);
d2=Math.pow(d1,-1);
textanswer.setText(""+d2);
first=true;
}
else if(event.getSource()==buttonAddAndSub)
{
String s=textanswer.getText();
if(s=="0.")
{
}
else
{
d2=Double.parseDouble(s);
d1=-d2;
textanswer.setText(""+d1);
}
}
else if(event.getSource()==buttonBk)
{
String s=textanswer.getText();
if(s.length()==1)
{
textanswer.setText("0.");
}
else if(s=="0.")
{
}
else if(s.length()>1)
{
textanswer.setText("");
for(int i=0;i<s.length()-1;i++)
{
char c=s.charAt(i);
textanswer.setText(textanswer.getText()+c);
}
}
first=true;
}
else if(event.getSource()==buttonCe)
{
textanswer.setText("0.");
first=true;
}
else if(event.getSource()==buttonC)
{
d1=d2=0;
textanswer.setText("0.");
first=true;
}
}
}
还有部分功能没有完成,待续