package test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
String[] key = {"7","8","9","/","4","5","6","*","1","2","3","-",".","0","=","+"};
String[] cmd = {"DEL", "C" };
JButton[] keyb= new JButton[key.length];
JPanel keybpanel=new JPanel(new GridLayout(4,4,3,3));
JPanel cmdpanel=new JPanel();
JButton cmdb[] = new JButton[cmd.length];
JTextField display = new JTextField(15);
JMenuBar menu=new JMenuBar();
JMenu xuanxiang,bangzhu=null;
JMenuItem a1,a2,b1,b2=null;
float first,second,result;
boolean check=true;
String operate;
boolean record=false;
public static void main(String[] args) {
new Calculator();
}
public Calculator()
{
xuanxiang=new JMenu("选项");
bangzhu=new JMenu("帮助");
a1=new JMenuItem("切换");
a2=new JMenuItem("退出");
b1=new JMenuItem("查看帮助");
b2=new JMenuItem("关于");
xuanxiang.add(a1);
xuanxiang.add(a2);
bangzhu.add(b1);
bangzhu.add(b2);
menu.add(xuanxiang);
menu.add(bangzhu);
for(int i=0;i<key.length;i++)
{
keyb[i]=new JButton(key[i]);
keyb[i].setForeground(Color.blue);
keybpanel.add(keyb[i]);
}
display.setEditable(false);
for(int i=0;i<cmd.length;i++)
{
cmdb[i]=new JButton(cmd[i]);
cmdb[i].setForeground(Color.red);
cmdpanel.add(cmdb[i]);
}
this.setJMenuBar(menu);
this.add("North",display);
this.add("Center",cmdpanel);
this.add("South",keybpanel);
this.setLocation(500,200);
this.setSize(274, 240);
this.setTitle("计算器");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
a1.addActionListener(this);
a2.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
for(int i=0;i<key.length;i++)
keyb[i].addActionListener(this);
for(int i=0;i<cmd.length;i++)
cmdb[i].addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
for(int i=0;i<key.length;i++)
{
if(e.getSource()==keyb[i]&&i!=14&&i!=3&&i!=7&&i!=11&&i!=15)
{
if(record==true&&e.getActionCommand()==".")
JOptionPane.showMessageDialog(null, "非法输入!");
else
display.setText(display.getText()+key[i]);
if(e.getActionCommand()==".")
{
record=true;
}
}
}
if(e.getSource()==keyb[7]||e.getSource()==keyb[3]||e.getSource()==keyb[11]||e.getSource()==keyb[15])
{
if(check==true)
{
first=Float.valueOf(display.getText()).floatValue();
display.setText("");
}
operate=e.getActionCommand();
check=false;
}
if(e.getSource()==keyb[14])
{
if(check==false)
{
second=Float.valueOf(display.getText()).floatValue();
if(operate.equals("+"))
result=first+second;
else if(operate.equals("-"))
result=first-second;
else if(operate.equals("/"))
result=first/second;
else if(operate.equals("*"))
result=first*second;
display.setText(String.valueOf(result));
}
else
{
JOptionPane.showMessageDialog(null, "您的输入存在错误!");
}
}
if(e.getSource()==cmdb[1])
{
check=true;
record=false;
display.setText("");
}
if(e.getSource()==cmdb[0])
display.setText("");
if(e.getSource()==a1)
JOptionPane.showMessageDialog(null, "暂时无法切换");
if(e.getSource()==a2)
System.exit(0);
if(e.getSource()==b1)
JOptionPane.showMessageDialog(null, "");
if(e.getSource()==b2)
JOptionPane.showMessageDialog(null, "版本号:v1.0\n作者:康彪\n学号:2013220202022\njava记事本实验作业");
}
}
下面是运行效果: