用NetBeans做简单的计算器
2015-09-20 20:34
411 查看
package jisuanqi;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class jsj extends javax.swing.JFrame {
public jsj() {
initComponents();
}
@SuppressWarnings("unchecked")
public void setNum(String str) {
//获得显示文本内容保存到strText
String strText = jTextField1.getText();
//如果文本内容为0,那么改为str;否则添加
if (strText.equals("0")) {
//那么改为str
jTextField1.setText(str);
} else {
// 添加
jTextField1.setText(strText + str);
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//实现数字的输入
private void jButton10MousePressed(java.awt.event.MouseEvent evt) {
setNum("0"); // TODO add your handling code here:
}
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
setNum("1"); // TODO add your handling code here:
}
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
setNum("2"); // TODO add your handling code here:
}
private void jButton3MousePressed(java.awt.event.MouseEvent evt) {
setNum("3"); // TODO add your handling code here:
}
private void jButton4MousePressed(java.awt.event.MouseEvent evt) {
setNum("4"); // TODO add your handling code here:
}
private void jButton5MousePressed(java.awt.event.MouseEvent evt) {
setNum("5"); // TODO add your handling code here:
}
private void jButton6MousePressed(java.awt.event.MouseEvent evt) {
setNum("6"); // TODO add your handling code here:
}
private void jButton7MousePressed(java.awt.event.MouseEvent evt) {
setNum("7"); // TODO add your handling code here:
}
private void jButton8MousePressed(java.awt.event.MouseEvent evt) {
setNum("8"); // TODO add your handling code here:
}
private void jButton9MousePressed(java.awt.event.MouseEvent evt) {
setNum("9"); //O add your handling code here:
}
private void dianMousePressed(java.awt.event.MouseEvent evt) {
setNum("."); // TODO add your handling code here:
}
//将文本框内的数据设置为0,用于全部清空
private void clearMousePressed(java.awt.event.MouseEvent evt) {
jTextField1.setText("0"); // TODO add your handling code here:
}
//加减乘除的实现
private void jiaMousePressed(java.awt.event.MouseEvent evt) {
setYSF("+"); // TODO add your handling code here:
}
private void jianMousePressed(java.awt.event.MouseEvent evt) {
setYSF("-"); // TODO add your handling code here:
}
private void chuMousePressed(java.awt.event.MouseEvent evt) {
setYSF("/"); // TODO add your handling code here:
}
//等于
private void dyMousePressed(java.awt.event.MouseEvent evt) {
ScriptEngineManager manager=new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("js");
Double result =null;
try {
result=(Double) se.eval("("+jTextField1.getText()+")*1.0");
// TODO add your handling code here:
} catch (ScriptException ex) {
Logger.getLogger(jsj.class.getName()).log(Level.SEVERE, null, ex);
jTextField1.setText("输入错误,重新输入");
}
jTextField1.setText(result.toString());
}
private void chengMousePressed(java.awt.event.MouseEvent evt) {
setYSF("*"); // TODO add your handling code here:
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//实现退格 用StringBuffer方法 如果str长度小于二就显示为0,否则就长度减一
private void qcMousePressed(java.awt.event.MouseEvent evt) {
String str=jTextField1.getText();
StringBuffer strBuff=new StringBuffer(str);
if (str.length()<2){jTextField1.setText("0");}
else {
strBuff.deleteCharAt(str.length()-1);
jTextField1.setText(strBuff.toString());
} // TODO add your handling code here:
}
public void setYSF(String str){
//取出当前显示的内容放入字符串变量strText中
String strText = jTextField1.getText();
//判断strText中字符串的最后一位是否为运算符
int lastRight =strText.length()-1;
char ch =strText.charAt(lastRight);
if (ch =='+'|| ch=='-'|| ch=='/' || ch=='*'){
//如果(){替换最右边的运算符为当前案件str。str放回文本框}否则{追加当前str‘}
String strResult =strText.substring(0,lastRight)+str;
//String strResult放回文本
jTextField1.setText(strResult);
}
else{
jTextField1.setText(strText+str);
//strText放回
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default loo
4000
k and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jsj().setVisible(true);
}
});
}
private javax.swing.JButton cheng;
private javax.swing.JButton chu;
private javax.swing.JButton clear;
private javax.swing.JButton dian;
private javax.swing.JButton dy;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton jia;
private javax.swing.JButton jian;
private javax.swing.JButton qc;
// End of variables declaration
}
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class jsj extends javax.swing.JFrame {
public jsj() {
initComponents();
}
@SuppressWarnings("unchecked")
public void setNum(String str) {
//获得显示文本内容保存到strText
String strText = jTextField1.getText();
//如果文本内容为0,那么改为str;否则添加
if (strText.equals("0")) {
//那么改为str
jTextField1.setText(str);
} else {
// 添加
jTextField1.setText(strText + str);
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//实现数字的输入
private void jButton10MousePressed(java.awt.event.MouseEvent evt) {
setNum("0"); // TODO add your handling code here:
}
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
setNum("1"); // TODO add your handling code here:
}
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
setNum("2"); // TODO add your handling code here:
}
private void jButton3MousePressed(java.awt.event.MouseEvent evt) {
setNum("3"); // TODO add your handling code here:
}
private void jButton4MousePressed(java.awt.event.MouseEvent evt) {
setNum("4"); // TODO add your handling code here:
}
private void jButton5MousePressed(java.awt.event.MouseEvent evt) {
setNum("5"); // TODO add your handling code here:
}
private void jButton6MousePressed(java.awt.event.MouseEvent evt) {
setNum("6"); // TODO add your handling code here:
}
private void jButton7MousePressed(java.awt.event.MouseEvent evt) {
setNum("7"); // TODO add your handling code here:
}
private void jButton8MousePressed(java.awt.event.MouseEvent evt) {
setNum("8"); // TODO add your handling code here:
}
private void jButton9MousePressed(java.awt.event.MouseEvent evt) {
setNum("9"); //O add your handling code here:
}
private void dianMousePressed(java.awt.event.MouseEvent evt) {
setNum("."); // TODO add your handling code here:
}
//将文本框内的数据设置为0,用于全部清空
private void clearMousePressed(java.awt.event.MouseEvent evt) {
jTextField1.setText("0"); // TODO add your handling code here:
}
//加减乘除的实现
private void jiaMousePressed(java.awt.event.MouseEvent evt) {
setYSF("+"); // TODO add your handling code here:
}
private void jianMousePressed(java.awt.event.MouseEvent evt) {
setYSF("-"); // TODO add your handling code here:
}
private void chuMousePressed(java.awt.event.MouseEvent evt) {
setYSF("/"); // TODO add your handling code here:
}
//等于
private void dyMousePressed(java.awt.event.MouseEvent evt) {
ScriptEngineManager manager=new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("js");
Double result =null;
try {
result=(Double) se.eval("("+jTextField1.getText()+")*1.0");
// TODO add your handling code here:
} catch (ScriptException ex) {
Logger.getLogger(jsj.class.getName()).log(Level.SEVERE, null, ex);
jTextField1.setText("输入错误,重新输入");
}
jTextField1.setText(result.toString());
}
private void chengMousePressed(java.awt.event.MouseEvent evt) {
setYSF("*"); // TODO add your handling code here:
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//实现退格 用StringBuffer方法 如果str长度小于二就显示为0,否则就长度减一
private void qcMousePressed(java.awt.event.MouseEvent evt) {
String str=jTextField1.getText();
StringBuffer strBuff=new StringBuffer(str);
if (str.length()<2){jTextField1.setText("0");}
else {
strBuff.deleteCharAt(str.length()-1);
jTextField1.setText(strBuff.toString());
} // TODO add your handling code here:
}
public void setYSF(String str){
//取出当前显示的内容放入字符串变量strText中
String strText = jTextField1.getText();
//判断strText中字符串的最后一位是否为运算符
int lastRight =strText.length()-1;
char ch =strText.charAt(lastRight);
if (ch =='+'|| ch=='-'|| ch=='/' || ch=='*'){
//如果(){替换最右边的运算符为当前案件str。str放回文本框}否则{追加当前str‘}
String strResult =strText.substring(0,lastRight)+str;
//String strResult放回文本
jTextField1.setText(strResult);
}
else{
jTextField1.setText(strText+str);
//strText放回
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default loo
4000
k and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(jsj.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jsj().setVisible(true);
}
});
}
private javax.swing.JButton cheng;
private javax.swing.JButton chu;
private javax.swing.JButton clear;
private javax.swing.JButton dian;
private javax.swing.JButton dy;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton jia;
private javax.swing.JButton jian;
private javax.swing.JButton qc;
// End of variables declaration
}
相关文章推荐
- java SE复习笔记30
- Java序列化的时候SerialVersion的作用
- java__基础03
- java基础之抽象类与接口的形式参数和返回值
- java基础之抽象类与接口的形式参数和返回值
- java SE复习笔记29
- java swing 界面
- Spring MVC 4.X ResponseBody 日期类型Json 处理
- 转 java 四种实现延迟加载的方法
- 解决myeclipse10.x的Servers产生的at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(Unknown Source)错
- Spring AOP基础知识
- java SE复习笔记28
- JAVA编程
- java异常
- Java中,&&与&,||与|的区别
- java SE复习笔记27
- JAVA中 @Override 的作用
- java SE复习笔记26
- 转 Java final、static使用总结
- Java中重载和重写的区别