java编写的计算器

package com.yc.api.day21;


import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class Hello {
private Display display;
private Shell shell;

private static final Logger LOG=Logger.getLogger(Hello.class);// 日志输出到全局的 即rootLogger 指定的文件中

//long start=System.currentTimeMillis();
public static void main(String[] args) 
{/*
System.out.println("Hello World");
String name = "zhfuxing";*/


Hello hello=new Hello();

hello.display=new Display();
hello.shell=new Shell(hello.display);
 
hello.creatContent();
hello.shell.open();
while(!hello.shell.isDisposed())
{
if(!hello.display.readAndDispatch())
{
hello.display.sleep();
}
}
 


}


private void creatContent() {
//简单计数器

shell.setBounds(300,300,600,400);


Label numLbl01=new Label(shell, SWT.NONE);
numLbl01.setBounds(200,100,100,20);
numLbl01.setText("请输入第一个数:");

final Text numText01=new Text(shell,SWT.NONE);
numText01.setBounds(305, 100, 150, 20);

Label numLbl02=new Label(shell, SWT.NONE);
numLbl02.setBounds(200,150,100,20);
numLbl02.setText("请输入第二个数:");

final Text numText02=new Text(shell,SWT.NONE);
numText02.setBounds(305, 150, 150, 20);



//加
Button addBt=new Button(shell,SWT.NONE);
addBt.setBounds(200, 200, 50, 20);
addBt.setText("+");

//减
Button mBt=new Button(shell,SWT.NONE);
mBt.setBounds(276, 200, 50, 20);
mBt.setText("-");

//乘
Button mutliBt=new Button(shell,SWT.NONE);
mutliBt.setBounds(342, 200, 50, 20);
mutliBt.setText("*");

//除
Button divBt=new Button(shell,SWT.NONE);
divBt.setBounds(400, 200, 50, 20);
divBt.setText("/");

Label resultLab=new Label(shell, SWT.NONE);
resultLab.setBounds(200,250,100,20);
resultLab.setText("   结   果:");

final Text resultTex=new Text(shell,SWT.NONE);
resultTex.setBounds(305, 250, 150, 20);

//加法运算
addBt.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent se)
{
try {
String numStr01=numText01.getText();
LOG.debug("numText01输入的数据:"+numStr01);
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num01=Integer.valueOf(numStr01);

String numStr02=numText02.getText();
LOG.debug("numText02输入的数据:"+numStr02);
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num02=Integer.valueOf(numStr02);

double sum=num01+num02;

LOG.debug("输出计算的结果:"+sum);
//resultTex.setText(Integer.toString(sum));//int类型转换为string
resultTex.setText(sum+"");
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
LOG.error("输入的不是数值..",e);
MessageBox mb=new MessageBox(shell);
mb.setText("计算错误");
mb.setMessage("请输入数值类型!");
mb.open();
}
}



});
//减法运算
mBt.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent se)
{
try{
String numStr01=numText01.getText();
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num01=Integer.valueOf(numStr01);

String numStr02=numText02.getText();
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num02=Integer.valueOf(numStr02);

double sum=num01-num02;
//resultTex.setText(Integer.toString(sum));
resultTex.setText(sum+"");
}
catch(NumberFormatException e)
{
LOG.error("输入的不是数值..",e);
MessageBox mb=new MessageBox(shell);
mb.setText("输入错误");
mb.setMessage("请输入正确的类型!!!");
mb.open();
}
}



});

//乘法运算
mutliBt.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent se)
{
try
{
String numStr01=numText01.getText();
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num01=Integer.valueOf(numStr01);

String numStr02=numText02.getText();
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num02=Integer.valueOf(numStr02);

double sum=num01*num02;
//resultTex.setText(Integer.toString(sum));
resultTex.setText(sum+"");
}
catch(NumberFormatException e)
{
LOG.error("输入的不是数值..",e);
MessageBox mb=new MessageBox(shell);
mb.setText("输入错误");
mb.setMessage("请输入正确的类型!!!");
mb.open();
}

}



});

//除法运算
divBt.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent se)
{
try
{
String numStr01=numText01.getText();
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num01=Integer.valueOf(numStr01);

String numStr02=numText02.getText();
//int num01=Integer.parseInt(numStr01);//把字符串转为int,借助包装类Integer
double num02=Integer.valueOf(numStr02);

double sum=num01/num02;
//resultTex.setText(Integer.toString(sum));
resultTex.setText(sum+"");
}
catch(NumberFormatException e)
{
LOG.error("输入的不是数值..",e);
MessageBox mb=new MessageBox(shell);
mb.setText("输入错误");
mb.setMessage("请输入正确的类型!!!");
mb.open();

}
catch(ArithmeticException e)
{
LOG.error("被除数不能为0",e);
MessageBox mb=new MessageBox(shell);
mb.setText("输入错误");
mb.setMessage("被除数不能为0");
mb.open();

}
}



});




}



}


日志文件如下:

log4j.rootLogger=DEBUG, stdout, fileout


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout =org.apache.log4j.TTCCLayout


log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.file=f:/logs/yc2015_6_30.log    --文件输出的位置
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=%d %5p %l %c %m %n

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值