浏览 2992 次
锁定老帖子 主题:基于 gwt 的简易计算器
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-05
分两个类写,一个显示(Calculator),一个计算(Operator)。 Calculator 类 public class Calculator implements EntryPoint { final VerticalPanel calculator = new VerticalPanel(); // 计算器管理面板 public void onModuleLoad() { final TextBox valueBox = new TextBox(); // 计算结果显示区 final Grid calculatorGrid = new Grid(4, 4); // 管理 Button 的网格 final String[] strBtns = new String[] { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", "C", "/", "=" }; // Button 的显示值 for (int i = 0; i < strBtns.length; i++) { calculatorGrid.setWidget(i / 4, i % 4, createButton(strBtns[i])); } // end for i valueBox.setText("0"); valueBox.setReadOnly(true); valueBox.setSize("135pt", "25pt"); valueBox.setTextAlignment(TextBoxBase.ALIGN_RIGHT); calculatorGrid.setCellSpacing(2); calculator.setTitle("Calculator"); calculator.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER); calculator.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM); calculator.setBorderWidth(5); calculator.add(valueBox); calculator.add(calculatorGrid); RootPanel.get().add(calculator); } // 创建 Button,并添加相关属性和事件 public Button createButton(String text) { Button button = new Button(text); button.setSize("30pt", "30pt"); // 添加 Button 的 onClick 事件 button.addClickListener(new ClickListener() { public void onClick(Widget widget) { String numSign = ((Button) widget).getText(); String value = ""; if ("0123456789".indexOf(numSign) != -1) { value = Operator.numClick(numSign); } else if ("+-*/".indexOf(numSign) != -1) { value = Operator.signClick(numSign); } else if ("C".indexOf(numSign) != -1) { value = Operator.cleanClick(numSign); } else if ("=".indexOf(numSign) != -1) { value = Operator.amountClick(); } ((TextBox)(calculator.getWidget(0))).setText(value); } }); return button; } } Operator 所有要传递的数据均需继承 com.google.gwt.user.client.rpc.IsSerializable 接口。 public class Operator implements IsSerializable { private static String old_Value = "0"; // 保存符号之前的值 private static String sign = ""; // 保存计算符号 private static String textValue = "0"; // 保存 valueBox 当前或将要显示的值 public static String getOld_Value() { return old_Value; } public static String textValue() { return textValue; } // 保存显示值 public static String numClick(String num) { if (textValue.equals("0") || textValue.equals("Error")) { textValue = num; } else { textValue += num; } return textValue; } // 保存计算符号 public static String signClick(String signStr) { sign = signStr; old_Value = textValue; textValue = "0"; return old_Value; } // 清空现有数据 public static String cleanClick(String clean) { sign = ""; old_Value = "0"; textValue = "0"; return textValue; } // 计算方法,并返回结果 public static String amountClick() { if (sign.equals("+")) { textValue = String.valueOf(Integer.decode(old_Value) + Integer.decode(textValue)); } else if (sign.equals("-")) { textValue = String.valueOf(Integer.decode(old_Value) - Integer.decode(textValue)); } else if (sign.equals("*")) { textValue = String.valueOf(Integer.decode(old_Value) * Integer.decode(textValue)); } else if (sign.equals("/")) { if (old_Value.equals("0")) { textValue = "Error"; } else { textValue = String.valueOf(Integer.decode(old_Value) / Integer.decode(textValue)); } } sign = ""; old_Value = "0"; return textValue; } } XML 配置文件 <module> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. You can change --> <!-- the theme of your GWT application by uncommenting --> <!-- any one of the following lines. --> <!-- <inherits name="com.google.gwt.user.theme.standard.Standard"/> --> <!-- <inherits name="com.google.gwt.user.theme.chrome.Chrome"/> --> <inherits name='com.google.gwt.user.theme.dark.Dark'/> <!-- Other module inherits --> <!-- Specify the app entry point class. --> <entry-point class='org.gwt.calculator.client.Calculator'/> <!-- Specify the application specific style sheet. --> <stylesheet src='Calculator.css' /> </module> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |