SWT/RAP计算器
/**
*2.测试
*/
public class NofTest extends AbstractEntryPoint {
Text text,text2;
RemoteObject remoteObject1;
Button testbtn;
@Override
protected void createContents(Composite parent) {
parent.setSize(400,400);
parent.setLocation(100,100);
parent.setVisible(false);
parent.setLayout(new GridLayout(3, false));
text = new Text(parent, SWT.BORDER);
text.setText("Hello");
//text.setText(RWT.getServiceManager().getServiceHandlerUrl("hello"));
text = new Text(parent, SWT.BORDER);
text.setText("Hello");
text.setText("测试");
//测试计算器
testbtn =new Button(parent, SWT.BORDER);
testbtn.setText("测试计算器");
testbtn.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
CalcDialog calTest = new CalcDialog(getShell());
calTest.open(true, new MyAsyncCallback() {
@Override
public void onSuccess(int id, Object... returnObjects) {
}
@Override
public void onFailure(int id, Throwable caught, Object... returnObjects) {
}
});
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import cn.lesaas.nof.rsclient.dialog.XAbstractDialog;
/*
*2.计算器类
*/
public class CalcDialog extends XAbstractDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int ID = -1;
/** 计算器上的键的显示名字 */
private final static String[] keys = { "Backspace", "CE", "C","+","7", "8", "9", "-", "4", "5", "6",
"*", "1", "2", "3", "/", "0","sqrt", ".", "=" };
/** 计算器上的功能键的显示名字 */
/** 计算结果文本框 */
private static Text resultText;
// 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private static boolean firstDigit = true;
// 计算的中间结果。
private static double resultNum = 0.0;
// 当前运算的运算符
private static String operator = "=";
// 操作是否合法
private static boolean operateValidFlag = true;
public CalcDialog(Shell parent) {
super(ID, parent);
}
@Override
protected void onCreateShell() {
setTitle("计算器");
setSize(400, 335);
}
@Override
protected void onCreateMainPanel(Composite parent) throws Exception {
Calc(parent);
}
/**
* 初始化计算器
*/
public static void Calc( Composite parent) {
parent=new Composite(parent, SWT.NONE);
//GridLayout gL=new GridLayout();
parent.setLayout(new GridLayout());
//创建数字显示框
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);//GridData是指定子控件的信息
resultText = new Text(parent,SWT.RIGHT|SWT.BORDER);
gridData.heightHint = 45;//初始高度为20
gridData.horizontalSpan = 3;//跨三列
resultText.setLayoutData(gridData);
resultText.setText("0.");
Composite composite=new Composite(parent, SWT.NONE);
GridLayout gridLayoutKey = new GridLayout(4,true);
gridLayoutKey.marginWidth = 0;//设置按钮距离父控件的左边距为0
gridLayoutKey.marginHeight = 0;//设置按钮距离父控件的右边距为0
composite.setLayout(gridLayoutKey);
GridData gridDataKey = new GridData(GridData.FILL_HORIZONTAL);
//gridDataKey.horizontalSpan = 3;
composite.setLayoutData(gridDataKey);
// 初始化功能键,都用红色标示。将功能键放在一个画板内
// 用网格布局器,1行,3列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
int length = keys.length;
Button buttonKey = null;
for(int i=0;i<length;i++){
buttonKey = new Button(composite,SWT.PUSH);
gridDataKey = new GridData(GridData.FILL_BOTH);
buttonKey.setLayoutData(gridDataKey);
buttonKey.setText(keys[i]);
buttonKey.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// 获取事件源的标签
Button button=(Button)e.getSource();
String btnText =button.getText();
//System.out.println(btnText+"====btnText");
if (btnText.equals(keys[0])) {
// 用户按了"Backspace"键
handleBackspace();
} else if (btnText.equals(keys[1])) {
// 用户按了"CE"键
resultText.setText("0");
} else if (btnText.equals(keys[2])) {
// 用户按了"C"键
handleC();
} else if ("0123456789.".indexOf(btnText) >= 0) {
// 用户按了数字键或者小数点键
handleNumber(btnText);
// handlezero(zero);
} else {
// 用户按了运算符键
handleOperator(btnText);
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {}
});
}
}
/**
* 处理Backspace键被按下的事件
*/
private static void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i > 0) {
// 退格,将文本最后一个字符去掉
text = text.substring(0, i - 1);
if (text.length() == 0) {
// 如果文本没有了内容,则初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
// 显示新的文本
resultText.setText(text);
}
}
}
/**
* 处理数字键被按下的事件
*
* @param key
*/
private static void handleNumber(String key) {
if (firstDigit) {
// 输入的第一个数字
resultText.setText(key);
} else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) {
// 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
// 如果输入的不是小数点,则将数字附在结果文本框的后面
resultText.setText(resultText.getText() + key);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
/**
* 处理C键被按下的事件
*/
private static void handleC() {
// 初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
}
/**
* 处理运算符键被按下的事件
*
* @param key
*/
private static void handleOperator(String key) {
//operator = key;
if (operator.equals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0) {
// 操作不合法
operateValidFlag = false;
resultText.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
// 倒数运算
if (resultNum == 0.0) {
// 操作不合法
operateValidFlag = false;
resultText.setText("零没有倒数");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")) {
// 加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")) {
// 减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")) {
// 乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// 平方根运算
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")) {
// 百分号运算,除以100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")) {
// 正数负数运算
resultNum = resultNum * (-1);
} else if (operator.equals("=")) {
// 赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
// 运算符等于用户按的按钮
operator = key;
firstDigit = true;
operateValidFlag = true;
}
/**
* 从结果文本框中获取数字
*
* @return
*/
private static double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e) {
}
return result;
}
@Override
public boolean isFavour(int keyCode, int stateMask) {
// TODO Auto-generated method stub
return false;
}
@Override
public void doShortCut(int keyCode, int stateMask) {
// TODO Auto-generated method stub
}
}
运行结果: