模拟计算器:
实现了加减乘除,取倒数,求平方根 运算,实现了退格,清除的功能。(增加取余操作%)
其中设置的MC,MR,MS,M+,+/-按钮尚未实现其功能。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestCounter {
String first=""; //第一个数
String operation=""; //得到按钮上的运算符
JFrame jf=new JFrame("计算器"); //对计算器窗口进行设置
JMenuBar jm=new JMenuBar();
JMenu jedit=new JMenu("编辑(E)");
JMenu jlook=new JMenu("查看(V)");
JMenu jhelp=new JMenu("帮助(H)");
JMenuItem jcopy=new JMenuItem("复制(Ctrl+c)");
JMenuItem jx= new JMenuItem("粘贴(Ctrl+v)");
JMenuItem jt=new JMenuItem("标准型(T)");
JMenuItem js=new JMenuItem("科学型(S)");
JMenuItem jh=new JMenuItem("帮助主题(H)");
JMenuItem ja=new JMenuItem("关于计算器(A)");
JPanel jp1=new JPanel();
JTextField jtf=new JTextField("0.",20);
JPanel jp2=new JPanel();
JPanel pnorth=new JPanel();
JPanel pwest=new JPanel();
JPanel pcenter=new JPanel();
JButton bback = new JButton("Backspace");
JButton bce = new JButton("CE");
JButton bc = new JButton("C"); //清除按钮
JButton bmc = new JButton("MC");
JButton bmr = new JButton("MR");
JButton bms = new JButton("MS");
JButton bm = new JButton("M+");
public TestCounter() {
jedit.add(jcopy);
jedit.add(jx);
jlook.add(jt);
jlook.add(js);
jhelp.add(jh);
jhelp.add(ja);
jp1.add(jtf);
jp2.setLayout(new BorderLayout());
jp2.add(pnorth,BorderLayout.NORTH);
jp2.add(pwest,BorderLayout.WEST);
jp2.add(pcenter,BorderLayout.CENTER);
pnorth.setLayout(new GridLayout(1,3,2,2));
pnorth.add(bback);
pnorth.add(bce);
pnorth.add(bc);
pwest.setLayout(new GridLayout(4, 1, 2, 2));
pwest.add(bmc);
pwest.add(bmr);
pwest.add(bms);
pwest.add(bm);
// pcenter添加按钮
pcenter.setLayout(new GridLayout(4, 5, 2, 2));
String[] array = { "7", "8", "9", "/", "Sqrt", "4", "5", "6", "*", "%",
"1", "2", "3", "-", "1/X", "0", "+/-", ".", "+", "=" };
String operation=""; //得到按钮上的运算符
JFrame jf=new JFrame("计算器"); //对计算器窗口进行设置
JMenuBar jm=new JMenuBar();
JMenu jedit=new JMenu("编辑(E)");
JMenu jlook=new JMenu("查看(V)");
JMenu jhelp=new JMenu("帮助(H)");
JMenuItem jcopy=new JMenuItem("复制(Ctrl+c)");
JMenuItem jx= new JMenuItem("粘贴(Ctrl+v)");
JMenuItem jt=new JMenuItem("标准型(T)");
JMenuItem js=new JMenuItem("科学型(S)");
JMenuItem jh=new JMenuItem("帮助主题(H)");
JMenuItem ja=new JMenuItem("关于计算器(A)");
JPanel jp1=new JPanel();
JTextField jtf=new JTextField("0.",20);
JPanel jp2=new JPanel();
JPanel pnorth=new JPanel();
JPanel pwest=new JPanel();
JPanel pcenter=new JPanel();
JButton bback = new JButton("Backspace");
JButton bce = new JButton("CE");
JButton bc = new JButton("C"); //清除按钮
JButton bmc = new JButton("MC");
JButton bmr = new JButton("MR");
JButton bms = new JButton("MS");
JButton bm = new JButton("M+");
public TestCounter() {
jedit.add(jcopy);
jedit.add(jx);
jlook.add(jt);
jlook.add(js);
jhelp.add(jh);
jhelp.add(ja);
jp1.add(jtf);
jp2.setLayout(new BorderLayout());
jp2.add(pnorth,BorderLayout.NORTH);
jp2.add(pwest,BorderLayout.WEST);
jp2.add(pcenter,BorderLayout.CENTER);
pnorth.setLayout(new GridLayout(1,3,2,2));
pnorth.add(bback);
pnorth.add(bce);
pnorth.add(bc);
pwest.setLayout(new GridLayout(4, 1, 2, 2));
pwest.add(bmc);
pwest.add(bmr);
pwest.add(bms);
pwest.add(bm);
// pcenter添加按钮
pcenter.setLayout(new GridLayout(4, 5, 2, 2));
String[] array = { "7", "8", "9", "/", "Sqrt", "4", "5", "6", "*", "%",
"1", "2", "3", "-", "1/X", "0", "+/-", ".", "+", "=" };
//对退格的处理
bback.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("0.");
}else{
jtf.setText(""+s.substring(0, s.length()-1));
}
}
});
for (int i = 0; i < array.length; i++) {
JButton jb= new JButton(array[i]);
pcenter.add(jb);
if(jb.getActionCommand().equals("7")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("7");
}else{
jtf.setText(s+"7");
}
}
});
}
if(jb.getActionCommand().equals("8")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("8");
}else{
jtf.setText(s+"8");
}
}
});
}
if(jb.getActionCommand().equals("9")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("9");
}else{
jtf.setText(s+"9");
}
}
});
}
if(jb.getActionCommand().equals(".")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText(".");
}else{
jtf.setText(s+".");
}
}
});
}
if(jb.getActionCommand().equals("4")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("4");
}else{
jtf.setText(s+"4");
}
}
});
}
if(jb.getActionCommand().equals("5")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("5");
}else{
jtf.setText(s+"5");
}
}
});
}
if(jb.getActionCommand().equals("6")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("6");
}else{
jtf.setText(s+"6");
}
}
});
}
if(jb.getActionCommand().equals("1")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("1");
}else{
jtf.setText(s+"1");
}
}
});
}
if(jb.getActionCommand().equals("2")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("2");
}else{
jtf.setText(s+"2");
}
}
});
}
if(jb.getActionCommand().equals("3")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("3");
}else{
jtf.setText(s+"3");
}
}
});
}
if(jb.getActionCommand().equals("0")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("0");
}else{
jtf.setText(s+"0");
}
}
});
}
//按下加号按钮
if(jb.getActionCommand().equals("+")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="+";
}
}
});
}
bback.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("0.");
}else{
jtf.setText(""+s.substring(0, s.length()-1));
}
}
});
for (int i = 0; i < array.length; i++) {
JButton jb= new JButton(array[i]);
pcenter.add(jb);
if(jb.getActionCommand().equals("7")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("7");
}else{
jtf.setText(s+"7");
}
}
});
}
if(jb.getActionCommand().equals("8")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("8");
}else{
jtf.setText(s+"8");
}
}
});
}
if(jb.getActionCommand().equals("9")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("9");
}else{
jtf.setText(s+"9");
}
}
});
}
if(jb.getActionCommand().equals(".")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText(".");
}else{
jtf.setText(s+".");
}
}
});
}
if(jb.getActionCommand().equals("4")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("4");
}else{
jtf.setText(s+"4");
}
}
});
}
if(jb.getActionCommand().equals("5")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("5");
}else{
jtf.setText(s+"5");
}
}
});
}
if(jb.getActionCommand().equals("6")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("6");
}else{
jtf.setText(s+"6");
}
}
});
}
if(jb.getActionCommand().equals("1")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("1");
}else{
jtf.setText(s+"1");
}
}
});
}
if(jb.getActionCommand().equals("2")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("2");
}else{
jtf.setText(s+"2");
}
}
});
}
if(jb.getActionCommand().equals("3")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("3");
}else{
jtf.setText(s+"3");
}
}
});
}
if(jb.getActionCommand().equals("0")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=jtf.getText();
if(s.equals("0.")){
jtf.setText("0");
}else{
jtf.setText(s+"0");
}
}
});
}
//按下加号按钮
if(jb.getActionCommand().equals("+")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="+";
}
}
});
}
//按下%号按钮
if(jb.getActionCommand().equals("%")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="%";
}
}
});
}
//按下 减 -按钮
if(jb.getActionCommand().equals("-")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="-";
}
}
});
}
//按下* 按钮
if(jb.getActionCommand().equals("*")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="*";
}
}
});
}
if(jb.getActionCommand().equals("%")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="%";
}
}
});
}
//按下 减 -按钮
if(jb.getActionCommand().equals("-")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="-";
}
}
});
}
//按下* 按钮
if(jb.getActionCommand().equals("*")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="*";
}
}
});
}
//按下 除 按钮
if(jb.getActionCommand().equals("/")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="/";
}
}
});
}
//按下平方根按钮
if(jb.getActionCommand().equals("Sqrt")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
operation="Sqrt";
}
}
});
}
//按下倒数按钮 1/X
if(jb.getActionCommand().equals("1/X")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
operation="1/X";
}
}
});
}
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
jtf.setText("0.");
operation="/";
}
}
});
}
//按下平方根按钮
if(jb.getActionCommand().equals("Sqrt")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
operation="Sqrt";
}
}
});
}
//按下倒数按钮 1/X
if(jb.getActionCommand().equals("1/X")){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jtf.getText(); //获取第一个数
if(!s1.equals("0.")){
first=s1; //把从JTextField中得到的第一个数暂存到first中
operation="1/X";
}
}
});
}
//"="的事件处理
if(jb.getActionCommand().equals("=")){
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s2=jtf.getText();//获取第二个数
if(!s2.equals("0.")){
//进行各种运算
// 求平方根
if(operation.equals("Sqrt")){
double sum=Math.sqrt(Double.valueOf(first));
jtf.setText(""+sum);
}
if(jb.getActionCommand().equals("=")){
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s2=jtf.getText();//获取第二个数
if(!s2.equals("0.")){
//进行各种运算
// 求平方根
if(operation.equals("Sqrt")){
double sum=Math.sqrt(Double.valueOf(first));
jtf.setText(""+sum);
}
//取余操作
if(operation.equals("%")){
int temp=(int)(Double.valueOf(first)/Double.valueOf(s2));
double sum=Double.valueOf(first)-temp*Double.valueOf(s2);
jtf.setText(""+sum);
}
if(operation.equals("%")){
int temp=(int)(Double.valueOf(first)/Double.valueOf(s2));
double sum=Double.valueOf(first)-temp*Double.valueOf(s2);
jtf.setText(""+sum);
}
//求倒数
if(operation.equals("1/X")){
double sum=1/Double.valueOf(first);
jtf.setText(""+sum);
}
//+
if(operation.equals("+")){
//将第一个数和第二个数转换成double类型进行运算
double sum=Double.valueOf(first)+Double.valueOf(s2);
jtf.setText(""+sum);
}
//*
if(operation.equals("*")){
double sum=Double.valueOf(first)*Double.valueOf(s2);
jtf.setText(""+sum);
}
// /
if(operation.equals("/")){
double sum=Double.valueOf(first)/Double.valueOf(s2);
jtf.setText(""+sum);
}
// -
if(operation.equals("-")){
double sum=Double.valueOf(first)-Double.valueOf(s2);
jtf.setText(""+sum);
}
}
}
});
}
}
//MenuBar里面添加Menu "编辑"查看""帮助""
jm.add(jedit);
jm.add(jlook);
jm.add(jhelp);
jf.add(jp1,BorderLayout.NORTH);
jf.add(jp2,BorderLayout.CENTER);
jf.setJMenuBar(jm);
jf.setSize(400, 400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭
//清除按钮事件触发
bc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf.setText("0.");
}
});
bce.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf.setText("0.");
}
});
}
public static void main(String[] args) {
new TestCounter();
}
}