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();
}
}
});
}
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