Package cn.lesaas.nof.rwtswt.ui.dialog;
Import Java.math.BigDecimal;
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.events.SelectionEvent;
Import Org.eclipse.swt.events.SelectionListener;
Import Org.eclipse.swt.layout.FormLayout;
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.Dialog;
Import org.eclipse.swt.widgets.Event;
Import Org.eclipse.swt.widgets.Listener;
Import Org.eclipse.swt.widgets.Shell;
Import Org.eclipse.swt.widgets.Text;
Import Cn.lesaas.nof.conn.base.MyAsyncCallback;
Import Cn.lesaas.nof.rsclient.UC;
/*
* Calculator class, no inherited modal window
*/
public class Calcdialog extends Dialog {
private int dialogid =-1;
/**
*
*/
Private static final long serialversionuid = 1L;
private static final int ID =-1;
The display name of the key on the/** calculator */
Private final static string[] keys = {"Backspace", "C", "+", "-", "*", "% Division Sign", "7", "8", "9", "4", "5", "6", "1",
"2", "3", "0", ".", "= Default Focus"};
The display name of the function key on the/** calculator */
/** Calculation result text box */
private static Text Resulttext;
private static Text labtopnum;//is stored in numbers and symbols
private static Text labdownresult;//the number of each input and two number of the
Flag whether the user presses the first number of the entire expression, or the first number after the operator
private static Boolean firstdigit = true;
The intermediate result of the calculation.
private static double resultnum = 0.0;
Operators for the current operation
private static String operator = "=";
Whether the operation is legal
private static Boolean Operatevalidflag = true;
private static Button Buttonkey = null;
Boolean closed;
Shell Shell;
Private Myasynccallback Cbopen;
Public Calcdialog (Shell parent) {
Super (parent, SWT. TITLE);
}
Create window Size
protected void Createshell () {
Shell = new Shell (getParent (), GetStyle ());
Shell.setlayout (New Formlayout ());
Oncreateshell ();
}
protected void Oncreateshell () {
Shell.settext ("calculator");
Shell.setsize (336, 380);
Shell.setsize (width, height);
Composite Composite = new Composite (Shell, SWT. NONE);
try {
Oncreatemainpanel (composite);
Oncreatebuttons (composite);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
protected void Oncreatemainpanel (Composite parent) throws Exception {
Calc (parent);
}
/**
* Initialize Calculator
*/
public static void Calc (Composite parent) {
Parent.setlayout (New GridLayout ());
Labtopnum = new Text (parent, SWT. Right | Swt. Read_Only | Swt. BORDER);//
Griddata Griddatalab = new Griddata (griddata.fill_horizontal);//Griddata is the information for the specified child control
Griddatalab.heighthint = 25;//initial height is 20
Griddatalab.horizontalspan = 3;//across three columns
Labtopnum.setlayoutdata (Griddatalab);
Labtopnum.settext ("");//only for storing numbers and symbols for each input
Labtopnum.setfont (Uc.getloginfont ());
Create a digital display box
Griddata griddata = new Griddata (griddata.fill_horizontal);//Griddata is the information for the specified child control
Resulttext = new Text (parent, SWT. Right | Swt. border| Swt. READ_ONLY);
Labdownresult = new Text (parent, SWT. Right | Swt. Read_Only | Swt. BORDER);//|
Swt. BORDER
Griddata.heighthint = 25;//initial height is 20
Griddata.horizontalspan = 3;//across three columns
Labdownresult.setlayoutdata (Griddata);
Labdownresult.settext ("");//store this as a logarithmic result.
Labdownresult.setfont (Uc.getloginfont ());
Labdownresult.setfocus ();
Composite Composite = new Composite (parent, SWT. NONE);
GridLayout Gridlayoutkey = new GridLayout (3, true);
Gridlayoutkey.marginwidth = 0;//Set button is 0 from the left margin of the parent control
Gridlayoutkey.marginheight = 0;//Set button is 0 from the right margin of the parent control
Composite.setlayout (Gridlayoutkey);
Griddata Griddatakey = new Griddata (griddata.fill_horizontal);
Griddatakey.horizontalspan = 3;
Composite.setlayoutdata (Griddatakey);
The initialization function keys are marked in red. Place function keys inside an artboard
With grid layout, 1 rows, 3 columns of grid, horizontal between the grid is 3 pixels, the vertical interval is 3 pixels
int length = Keys.length;
int i = 0;
for (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.forcefocus ();
Listener Listener = new Listener () {
public void Handleevent (Event e) {
if (E.type = = SWT. Selection) {
Button button = (button) E.widget;
String Btntext = Button.gettext ();
Gets the label of the event source
if (Btntext.equals ("Backspace")) {
The user pressed the "Backspace" key
Handlebackspace ();
} else if (Btntext.equals ("C")) {
The user pressed the "C" key
Handlec ();
} else if ("0123456789."). IndexOf (Btntext) >= 0) {
The user presses the number key or the decimal key
Toprecord (Btntext);
Handlenumber (Btntext);
Handlezero (zero);
} else if (btntext.equals ("+") | | Btntext.equals ("-") | | Btntext.equals ("*") | | Btntext.equals ("% Division sign") | | Btntext.equals ("= Default Focus")) {
The user presses the operator key
if (btntext.equals ("% Division Sign")) Btntext = "%";
if (btntext.equals ("= Default Focus")) Btntext = "=";
Boolean flag = Toprecord (Btntext);
if (flag) {
Handleoperator (Btntext);
}
}
}
}
};
Buttonkey.addlistener (SWT. Selection, listener);
}
Listener Listener = new Listener () {
public void Handleevent (Event e) {
if (E.type = = SWT. KeyDown) {
0-9.----131072+
char C = e.character;
String str = string.valueof (c);
if (c >= ' 0 ' && C <= ' 9 ' | | c = = '. ') {
String markstr = string.valueof (c);
Boolean flag = Toprecord (str);
if (flag) {
Handlenumber (str);
}
} else if ((c = = ' + ' && e.keycode = = 107) | | (c = = ' + ' && E.keycode = = 61) | | (c = = '-' && e.keycode = = 109) | | (c = = '-' && e.keycode = = 173) | | c = = ' * ' | | c = = '% ') {//61 is + also =,53 this button%,173 This button-,191 you click KeyDown this button/
c = e.character;//"\ r"
String markstr = string.valueof (c);
Boolean flag = Toprecord (MARKSTR);
if (flag) {
Handleoperator (MARKSTR);
}
} else if (E.keycode = = 108 | | e.keycode = = 13 | | (E.keycode = = 61&&c== ' = ')) {//specifically for Enter is =-=-e.keycode==187+=
String markstr = "=";
Boolean flag = Toprecord (MARKSTR);
if (flag) {
Handleoperator (MARKSTR);
}
Buttonkey.forcefocus ();
} else if (E.keycode = = 8) {//user pressed "Backspace" key
Handlebackspace ();
} else if (E.keycode = = 127 | | e.keycode = = ' C ') {//delete| | C
The user pressed the "C" key
Handlec ();
}
}
}
};
Registering a non-type event listener
Buttonkey.addlistener (SWT. Verify, listener);
Buttonkey.addlistener (SWT. KeyDown, listener);
Buttonkey.addlistener (SWT. Selection, listener);
}
/**
* Handling the event that the BACKSPACE key is pressed
*/
private static void Handlebackspace () {
String text = Labdownresult.gettext ();
int i = Text.length ();
if (i > 0) {
Backspace to remove the last character of the text
Text = text.substring (0, i-1);
if (text.length () = = 0) {
If the text does not have content, initialize the various values of the calculator
Labdownresult.settext ("0");
Firstdigit = true;
operator = "=";
} else {
Display the new text
Labdownresult.settext (text);
}
}
}
private static Boolean Toprecord (String key) {
char c = key.charat (0);
if (c >= ' 0 ' && C <= ' 9 ' &&c!= ') {
} else if (c = = ' + ' | | c = = '-' | | c = = ' * ' | | c = = '% ' | | c = = ') ') {
String downstr = Labdownresult.gettext ();
String top = Labtopnum.gettext ();
if (Downstr.isempty () && top.isempty () &&!key.equals ("=")) {
Labtopnum.settext (0 + key);
} else if (!downstr.isempty () &&!top.isempty () &&!key.equals ("=")) {
String Topmark = top.substring (Top.length ()-1, Top.length ());
Double Downparst = double.parsedouble (DOWNSTR);
If (!operator.equals ("=") &&!key.equals ("=") && Downparst = = Resultnum
&& ( Topmark.equals ("+") | | Topmark.equals ("-") | | Topmark.equals ("*") | | Topmark.equals ("%"))) {//12+1+-
String topfirst = top.substring (0, Top.length ()-1);
Labtopnum.settext (Topfirst + key);
//Labdownresult.settext (DOWNSTR);
operator = key;///replaced the symbol, but the symbol cannot be evaluated
return false;
} else {
String str = Labdownresult.gettext ();
String str=string.valueof (Resultnum);
if (Str.contains (".")) {
String first = str.substring (0, Str.indexof ("."));
String last = str.substring (Str.indexof (".") + 1, str.length ());
if (Last.equals ("0")) {
Labtopnum.settext (top + first + key);
} else {
Labtopnum.settext (top + str + key);
}
} else {
Labtopnum.settext (top + str + key);
}
}
//Labtopnum.settext (Top+str+key),
} else if (!downstr.isempty () &&!top.isempty () && Operator.equals ("=") &&!key.equals ("=")) {
String Str=labdownresult.gettext ();
String str = string.valueof (resultnum);
if (Str.contains (".")) {
String first = str.substring (0, Str.indexof ("."));
String last = str.substring (Str.indexof (".") + 1, str.length ());
if (Last.equals ("0")) {
Labtopnum.settext (top + first + key);
} else {
Labtopnum.settext (top + str + key);
}
} else {
Labtopnum.settext (str + key);
}
Labtopnum.settext (Top+str+key);
}
else if (!downstr.isempty () && top.isempty () &&!key.equals ("=")) {
String str = Labdownresult.gettext ();
if (Str.contains (".")) {
String first = str.substring (0, Str.indexof ("."));
String last = str.substring (Str.indexof (".") + 1, str.length ());
if (Last.equals ("0")) {
Labtopnum.settext (first + key);
} else {
Labtopnum.settext (str + key);
}
} else {
Labtopnum.settext (str + key);
}
} else if (key.equals ("=")) {
Labtopnum.settext ("");
}
}
return true;
}
private static void Handlenumber (String key) {
if (Firstdigit&&key.equals (".")) {
The first number entered
Labdownresult.settext (0+key);
}else if (firstdigit) {
Labdownresult.settext (key);
}else if ((Key.equals (")") && (Labdownresult.gettext (). IndexOf (".") < 0)
&&!labdownresult.gettext (). Contains (".")) {
The decimal point is entered, and there is no previous decimal point, the decimal point is appended to the result text box
Labdownresult.settext (Labdownresult.gettext () + ".");
} else if (!key.equals (".")) {
If the input is not a decimal point, the number is appended to the result text box
Labdownresult.settext (Labdownresult.gettext () + key);
}
It's definitely not the first number to be entered later.
Firstdigit = false;
}
/**
* Handle the event that the C key is pressed
*/
private static void Handlec () {
String txtnum = Labtopnum.gettext ();
if (!txtnum.isempty ()) {
Labtopnum.settext ("");
}
Initialize the various values of the calculator
Resultnum = 0.0;//In order to clear the last reserved static value
Labdownresult.settext ("0");
Firstdigit = true;
operator = "=";
}
/**
* Handling events where operator keys are pressed
*
* @param key
*/
private static void Handleoperator (String key) {
operator = key;
if (operator.equals ("%")) {
Division operation
If the value in the current result text box equals 0
if (getnumberfromtext () = = 0.0) {
Operation not valid
Operatevalidflag = false;
Labdownresult.settext ("Divisor cannot be zero");
} else {
if (key.equals ("%"))
Key = "/";
Resultnum/= Getnumberfromtext ();
Resultnum = div (resultnum, getnumberfromtext ());
}
} else if (operator.equals ("+")) {
Addition operation
Resultnum + = Getnumberfromtext ();
Resultnum = Add (Resultnum, Getnumberfromtext ());
} else if (Operator.equals ("-")) {
Subtraction operations
Resultnum-= Getnumberfromtext ();
Resultnum = Sub (resultnum, Getnumberfromtext ());
} else if (Operator.equals ("*")) {
Multiplication operations
Resultnum *= Getnumberfromtext ();
Resultnum = Mul (Resultnum, Getnumberfromtext ());
} else if (operator.equals ("=")) {
Assignment Operation 11+23+55;--resultnum=8--+3===-1+2+5+3
Resultnum = Getnumberfromtext ();
Labtopnum.settext ("");
}
if (Operatevalidflag) {
Operation of double-precision floating-point numbers
Long T1;
Double T2;
T1 = (long) resultnum;
t2 = resultnum-t1;
if (t2 = = 0) {
Labdownresult.settext (string.valueof (t1));
} else {
Labdownresult.settext (string.valueof (resultnum));
}
}
Operator equals the button that the user presses
operator = key;
Firstdigit = true;
Operatevalidflag = true;
}
/**
* Get numbers from the result text box
*
* @return
*/
private static Double Getnumberfromtext () {
Double result = 0;
String down = Labdownresult.gettext ();
try {
result = Double.valueof (down). Doublevalue ();
} catch (NumberFormatException e) {
}
return result;
}
private static final int def_div_scale = 10;
/**
* * Two double numbers added *
*
* @param v1
* *
* @param v2
* *
* @return Double
*/
public static double Add (double v1, double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
return new Double (B1.add (B2). Doublevalue ());
}
/**
* * Two double number subtraction *
*
* @param v1
* *
* @param v2
* *
* @return Double
*/
public static double sub (double V1, double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
return new Double (B1.subtract (B2). Doublevalue ());
}
/**
* * Two double multiply *
*
* @param v1
* *
* @param v2
* *
* @return Double
*/
public static double Mul (double v1, double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
return new Double (B1.multiply (B2). Doublevalue ());
}
/**
* * Two double number divide * Subtraction in Java
*
* @param v1
* *
* @param v2
* *
* @return Double
*/
public static double div (double v1, double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
return new Double (B1.divide (B2, Def_div_scale, bigdecimal.round_half_up). Doublevalue ());
}
// /**
* * Divide two double numbers and leave the scale decimal number *
// *
* @param v1 *
* @param v2 *
* @param scale *
* @return Double
// */
public static double div (double v1, double v2, int scale) {
if (Scale < 0) {
throw New IllegalArgumentException (
"The scale must is a positive integer or zero");
// }
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
return new Double (B1.divide (B2, scale,
BIGDECIMAL.ROUND_HALF_UP). Doublevalue ());
// }
public void Open (Boolean iscenter, Myasynccallback cbopen) {
This.cbopen = Cbopen;
closed = false;
try {
Createshell ();
if (!closed)
Shell.open ();
if (iscenter) {
Uc.centershell (shell);
Oncreateshell (shell);
}
} catch (Exception e) {
E.printstacktrace ();
Cbopen.onfailure (dialogid,e);
}
}
protected void Oncreatebuttons (Composite Composite) {
Button button = New button (composite, SWT. BORDER);
Griddata gridData1 = new Griddata (SWT. FILL, SWT. FILL, True, false);
Button.setlayoutdata (GRIDDATA1);
Button.setbackground (Uc.getcolor ());
Button.settext ("Off");
Button.addselectionlistener (New Selectionlistener () {
@Override
public void widgetselected (Selectionevent arg0) {
Resultnum = 0.0;
Close ();
}
@Override
public void widgetdefaultselected (Selectionevent arg0) {
TODO auto-generated Method Stub
}
});
}
Public final void Close () {
closed = true;
Shell.close ();
}
}
Java Swt/rap Calculator version 2 (keyboard mouse compatible)