您的位置:首页 > 编程语言 > Java开发

Java基础篇(一)-----计算器

2016-06-19 23:41 411 查看
package caculator;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Caculator extends WindowAdapter{
//定义3个面板
Panel p1=new Panel();		//创建面板1
Panel p2=new Panel();		//创建面板2
Panel p3=new Panel();       //创建面板3
TextField txt;				//创建文本框对象
private Button[] b=new Button[17];//创建按钮数组
private String ss[]={"7","8","9","+","4","5","6","-","1","2","3","*","清空","0","=","/","关闭"};//创建字符串数组
static double a=0.0;    //创建double类型变量
static String s,str,s1=" "; //创建String类型变量
public static void main(String[] args) throws Exception {
(new Caculator()).frame();
}
public void frame() {//实现界面
Frame fm=new Frame("计算器");  //创建窗口对象
for(int i=0;i<=16;i++) {
b[i]=new Button(ss[i]); //为按钮数组赋值
}
for(int i=0;i<=15;i++){
p2.add(b[i]);			//添加按钮到面板上去
}
//b[16].setBackground(Color.yellow);//设置按钮的背景色为黄色
//创建和设置文本框
txt=new TextField(15);
txt.setEditable(false);
for(int i=0;i<=16;i++) {
b[i].addActionListener(new buttonlistener());//为按钮添加监听器
}
b[16].addActionListener(new close()); //为按钮添加关闭监听器
fm.addWindowListener(this);
fm.setBackground(Color.red); //设置窗口背景为红色
p1.setLayout(new BorderLayout());//设置面板p1布局管理器
p1.add(txt, "North");//添加文本框到北面部分
p2.setLayout(new GridLayout(4,4));//设置面板p2布局管理器
p3.setLayout(new BorderLayout()); //设置面板p3布局管理器
p3.add(b[16]); //添加按钮到面板p3
//添加各个面板到窗口
fm.add(p1,"North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);
}
public void windowClosing(WindowEvent e) {
System.exit(0);   //退出系统
}
//编写事件监听器
class buttonlistener implements ActionListener {
public void actionPerformed(ActionEvent e){
Button btn=(Button)e.getSource(); //获取发生事件按钮
if(btn.getLabel()=="="){
jisuan();
str=String.valueOf(a);
txt.setText(str);
s="";
s1="";
}else if(btn.getLabel()=="+") {
jisuan();
txt.setText("");
s="+";
s1="";
}else if(btn.getLabel()=="-"){
jisuan();
txt.setText("");
s="-";
s1="";
}else if(btn.getLabel()=="*"){
jisuan();
txt.setText("");
s="*";
s1="";
}else if(btn.getLabel()=="/"){
jisuan();
txt.setText("");
s="/";
s1="";
}else if(btn.getLabel()=="0"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="1"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="2"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="3"){
txt.setText("3");
}else if(btn.getLabel()=="4"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="5"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="6"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="7"){
s1+=btn.getLabel();
txt.setText(s1);
}else if(btn.getLabel()=="8"){
s1+=btn.getLabel();
txt.setText(s1);
}
else if(btn.getLabel()=="9"){
s1+=btn.getLabel();
txt.setText(s1);
}else {
txt.setText("0");
s1="";
}

}
public void jisuan(){
if(s=="+")
a+=Double.parseDouble(txt.getText());
else if(s=="-")
a-=Double.parseDouble(txt.getText());
else if(s=="*")
a*=Double.parseDouble(txt.getText());
else if(s=="/")
a/=Double.parseDouble(txt.getText());
else
a=Double.parseDouble(txt.getText());
}
}
class close implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
}
简单演示:8+456=464.0
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 计算器