⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cump.java

📁 用SWT做的一个计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				if (text.getText() != ""){
					num1 =Double.parseDouble(text.getText());
					temp = "mul";
					clickable = false;
				}
			}
		});
		button_Mul.setText("*");
		button_Mul.setBounds(109, 39, 35, 30);

//除法运算
		final Button button_Div = new Button(composite, SWT.NONE);
		button_Div.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (text.getText() != ""){
					num1 =Double.parseDouble(text.getText());
					temp = "div";
					clickable = false;
				}
			}
		});
		button_Div.setText("/");
		button_Div.setBounds(109, 6, 35, 30);

//平方根运算
		final Button sqrtButton = new Button(composite, SWT.NONE);
		sqrtButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String s = text.getText();
		          if (s.charAt(0) == '-') {
		            text.setText("负数不能开根号");
		            clickable = false;
		          } else {
		            text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText()))));
		          }
			}
		});
		sqrtButton.setText("Sqrt");
		sqrtButton.setBounds(145, 6, 35, 30);		

//取模运算
		final Button button_Mod = new Button(composite, SWT.NONE);
		button_Mod.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				
			}
		});
		button_Mod.setText("%");
		button_Mod.setBounds(145, 39, 35, 30);
		
//等于运算		
		final Button button_Equal = new Button(composite, SWT.NONE);
		button_Equal.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (temp != null && text.getText() != ""){
					num2 = Double.parseDouble(text.getText());
					if (temp == "add"){
						text.setText(Double.toString(num1 + num2));
						//temp = null;
					}else if (temp == "sub"){
						text.setText(Double.toString(num1 - num2));
						//temp = null;
					}else if (temp == "mul"){
						text.setText(Double.toString(num1 * num2));
						//temp = null;
					}else if (temp == "div"){
						text.setText(Double.toString(num1 / num2));
						//temp = null;
					}
					clickable = false;
				}
			}
		});
		button_Equal.setText("=");
		button_Equal.setBounds(145, 72, 35, 63);


//取反运算
		final Button button_AddAndSub = new Button(composite, SWT.NONE);
		button_AddAndSub.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				boolean isNumber = true;
		          String s = text.getText();
		          for (int i = 0; i < s.length(); i++)
		            if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'
		                  || s.charAt(i) == '.' || s.charAt(i) == '-')) {
		                isNumber = false;
		                break;
		            }
		          if (isNumber == true) {
		            //如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉
		            if (s.charAt(0) == '-') {
		                text.setText("");
		                for (int i = 1; i < s.length(); i++) {
		                  char a = s.charAt(i);
		                  text.setText(text.getText() + a);
		                }
		            }
		            //如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
		            else
		                text.setText('-' + s);
		          }
			}
		});
		button_AddAndSub.setText("+/-");
		button_AddAndSub.setBounds(37, 105, 35, 30);
		

//BackSpace
		final Button backButton = new Button(composite_1, SWT.NONE);
		backButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (text.getText() != "") {
			          String s = text.getText();
			          text.setText("");
			          for (int i = 0; i < s.length() - 1; i++) {
			            char a = s.charAt(i);
			            text.setText(text.getText() + a);
			          }
				}
			}
		});
		backButton.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		backButton.setText("Backspace");
		backButton.setBounds(46, 0, 87, 30);
//CE
		final Button ceButton = new Button(composite_1, SWT.NONE);
		ceButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (text.getText() != ""){
					text.setText("");
				}
			}
		});
		ceButton.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		ceButton.setText("CE");
		ceButton.setBounds(134, 0, 46, 30);
//C
		final Button cButton = new Button(composite_1, SWT.NONE);
		cButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				text.setText("");
				num1 = 0;
				temp = null;
			}
		});
		cButton.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		cButton.setText("C");
		cButton.setBounds(181, 0, 44, 30);

//M-Text
		text_Memory = new Text(composite_1, SWT.READ_ONLY | SWT.CENTER | SWT.BORDER);
		text_Memory.setForeground(SWTResourceManager.getColor(0, 0, 255));
		text_Memory.setFont(SWTResourceManager.getFont("", 14, SWT.BOLD));
		text_Memory.setBounds(8, 5, 25, 25);


//MC
		final Button mcButton = new Button(composite_2, SWT.NONE);
		mcButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				memoryd = memoryi = 0;
		        text_Memory.setText("");
			}
		});
		mcButton.setFont(SWTResourceManager.getFont("Arial", 14, SWT.BOLD));
		mcButton.setText("MC");
		mcButton.setBounds(0, 6, 40, 30);
//MR
		final Button mrButton = new Button(composite_2, SWT.NONE);
		mrButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (memoryd != 0) {
		            text.setText(Double.toString(memoryd));
				}
		        if (memoryi != 0) {
		            text.setText(Integer.toString(memoryi));
		        }
		        clickable = false;
			}
		});
		mrButton.setFont(SWTResourceManager.getFont("Arial", 14, SWT.BOLD));
		mrButton.setText("MR");
		mrButton.setBounds(0, 39, 40, 30);
//MS
		final Button mButton_Sub = new Button(composite_2, SWT.NONE);
		mButton_Sub.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (text.getText() != ""){
					boolean isDot = false;
					text_Memory.setText("M");
					for (int i = 0; i < text.getText().length(); i++){
			            if ('.' == text.getText().charAt(i)) {
			                isDot = true;
			                break;
			            }
					}
					//如果是double,则存入memoryd(double存储器)
					if (isDot == true) {
						memoryd = Double.parseDouble(text.getText());
						memoryi = 0; //保证存储器中存放最新的值
					}
					//如果是int,则存入memoryi(int存储器)
					else {
						memoryi = Integer.parseInt(text.getText());
						memoryd = 0; //保证存储器中存放最新的值
					}
					clickable = false;
				}
			}
		});
		mButton_Sub.setFont(SWTResourceManager.getFont("Arial", 14, SWT.BOLD));
		mButton_Sub.setText("MS");
		mButton_Sub.setBounds(0, 72, 40, 30);
//M+
		final Button mButton_Add = new Button(composite_2, SWT.NONE);
		mButton_Add.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (text.getText() != ""){
					boolean isDot = false;
					for (int i = 0; i < text.getText().length(); i++){
						if ('.' == text.getText().charAt(i)) {
							isDot = true;
							break;
						}
					}
					if (memoryi != 0) { //存储中是一个int型数
						if (isDot == false) //被加数是一个int型数
							memoryi += Integer.parseInt(text.getText());
						else { //被加数是一个double型数,则将int存储器中数传入double存储器与当前数相加,int存储器清零
							memoryd = memoryi + Double.parseDouble(text.getText());
							memoryi = 0;
						}
					} else {
						memoryd += Double.parseDouble(text.getText());
					}
					clickable = false;
				}
			}
		});
		mButton_Add.setFont(SWTResourceManager.getFont("Arial", 14, SWT.BOLD));
		mButton_Add.setText("M+");
		mButton_Add.setBounds(0, 105, 40, 30);
		
		
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -