java计算器源代码有注解
JAVA简易计算器程序源代码
JAVA简易计算器程序源代码
package myText;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Calculator extends JFrame implements ActionListener{ private JPanel Panel1=new JPanel();
private JPanel Panel2=new JPanel();
private JTextField tfResult=new JTextField(25);
private JLabel label=new JLabel("计算结果:");
private String recentOperation=null;
private String recentNum=null;
private boolean isNew=true;
public void addButton(Container c,String s){
JButton b=new JButton(s);
b.setFont(new java.awt.Font("SansSerif",0,12));
b.setForeground(Color.red);
b.setBorder(BorderFactory.createRaisedBevelBorder());
c.add(b);
b.addActionListener(this);
计算器的Java源代码
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Calculator extends JFrame implements ActionListener{JPanel panel1=new JPanel();JPanel panel2=new JPanel();JTextField tfMemory=new JTextField();JTextField tfResult=new JTextField();//定义数字按键JButton button_0=new JButton();JButton button_1=new JButton();JButton button_2=new JButton();JButton button_3=new JButton();JButton button_4=new JButton();JButton button_5=new JButton();JButton button_6=new JButton();JButton button_7=new JButton();JButton button_8=new JButton();JButton button_9=new JButton();//定义四则运算按键JButton button_add=new JButton();JButton button_sub=new JButton();JButton button_mul=new JButton();JButton button_div=new JButton();JButton button_dot=new JButton();JButton button_equal=new JButton();//定义三角函数按键JButton button_sin=new JButton();JButton button_cos=new JButton();JButton button_tan=new JButton();//定义其他运算按键JButton button_sqrt=new JButton();JButton button_daoshu=new JButton();JButton button_pow=new JButton();//定义域存储器相关的按键JButton button_MC=new JButton();JButton button_MR=new JButton();JButton button_MS=new JButton();JButton button_MPlus=new JButton();JButton button_MMinus=new JButton();//定义两个常数按键JButton button_PI=new JButton();JButton button_E=new JButton();//定义符号键和清零键JButton button_sign=new JButton();JButton button_CE=new JButton();//定义布局方式GridLayout grid1=new GridLayout();GridBagLayout gridbag1=new GridBagLayout();GridBagLayout gridbag2=new GridBagLayout();//保存最近一次运算符String recentOperation=null;//保存最近一次的运算数据String recentNum=null;//描述当前输入状态,是重新输入还是接在后面,重新输入时为trueboolean isNew=true;public Calculator(){//为按键注册监听器button_0.addActionListener(this);button_1.addActionListener(this);button_2.addActionListener(this);button_3.addActionListener(this);button_4.addActionListener(this);button_5.addActionListener(this);button_6.addActionListener(this);button_7.addActionListener(this);button_8.addActionListener(this);button_9.addActionListener(this);button_add.addActionListener(this);button_sub.addActionListener(this);button_mul.addActionListener(this);button_div.addActionListener(this);button_dot.addActionListener(this);button_equal.addActionListener(this);button_sin.addActionListener(this);button_cos.addActionListener(this);button_tan.addActionListener(this);button_sqrt.addActionListener(this);button_daoshu.addActionListener(this);button_pow.addActionListener(this);button_MC.addActionListener(this);
计算器JAVA源代码
importjava.awt.*; //引入awt包
importjavax.swing.*; //引入javax.swing包
importjava.awt.event.*; //引入awt.event包
public class TheCalculator extends WindowAdapter
//程序框架继承自WindowAdapter类(窗体适配器)
{
private JTextFieldJtext=new JTextField("");//程序初始化文本框的内容为空
private JFrame f=new JFrame("计算器");//Jframe窗体名为“计算器”
private String a=""; //声明变量类型
private String b="";
private String cal="";
private boolean flag1=true; //声明变量flag1对象类型为布尔型
private boolean flag2=false; //声明变量flag2对象类型为布尔型
public void init() //初始化
{
String[] buttonValue = new String[]{"1","2","3","+","C","4","5","6","-","退格","7","8","9","*","1/x","0","+/-",".","/","="};
Container contain = f.getContentPane();//初始化窗体
JPanelJpan = new JPanel(); //实例化面板
java计算器代码
java计算器代码import java.awt.*;
import java.awt.event.*;
class leiyi extends Frame {
static double con1=0;
static int jihao=0;
static int xiao=0;
static int jishu=10;
private Button button1=new Button("7"); private Button button2=new Button("8"); private Button button3=new Button("9"); private Button button4=new Button("+"); private Button button5=new Button("4"); private Button button6=new Button("5"); private Button button7=new Button("6"); private Button button8=new Button("-"); private Button button9=new Button("1"); private Button button10=new Button("2"); private Button button11=new Button("3"); private Button button12=new Button("="); private Button button13=new Button("*"); private Button button14=new Button("/"); private Button button15=new Button("0");
java简单计算器--代码
//中南民大08网工二班mzimport java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.JOptionPane;public class Calculator extends Applet implements ActionListener{Button backButton=new Button("退格");Button clearButton=new Button("清除");Panel p1 = new Panel(new FlowLayout());Panel p2 = new Panel(new GridLayout(2,1));Panel p3 = new Panel(new GridLayout(4,5,5,5));TextField t=new TextField("0");float num1=0;char ch='#';boolean can=false;public void init(){t.setFont(new Font("宋体",Font.BOLD,25));backButton.setFont(new Font("黑体",Font.BOLD,15));backButton.setForeground(Color.red);clearButton.setFont(new Font("黑体",Font.BOLD,15));clearButton.setForeground(Color.red);p1.add(backButton);p1.add(clearButton);backButton.addActionListener(this);clearButton.addActionListener(this);p2.add(t);p2.add(p1);p2.setBackground(Color.black);p3.setBackground(Color.black);this.setLayout(new BorderLayout());this.add(p2,"North");this.add(p3,"Center");String buttonStr = "789/A456*B123-C0.D+=";for (int i = 0; i < buttonStr.length(); i++)this.addButton(p3,buttonStr.substring(i, i + 1));}private void addButton(Container c, String s){Button b = new Button(s);if (s.equals("A"))b.setLabel("sqrt");else if (s.equals("B"))b.setLabel("1/x");else if (s.equals("C"))b.setLabel("%");else if (s.equals("D"))b.setLabel("+/-");b.setForeground(Color.blue);b.setFont(new Font("黑体",Font.BOLD,15));c.add(b);b.addActionListener(this);}public void actionPerformed(ActionEvent e){String act=e.getActionCommand();if(e.getSource()==backButton){if(t.getText().length()>1)t.setText(t.getText().substring(0, t.getText().length()-1));else t.setText("0");return;}if(e.getSource()==clearButton){t.setText("0");ch='#';return;}if(act.equals("+/-")){if(t.getText().charAt(0)!='-')t.setText("-"+t.getText());else t.setText(t.getText().substring(1));return;}if(act.equals(".")){t.setText(t.getText()+act);return;}if(act!="1/x"&&act.charAt(0)>='0'&&act.charAt(0)<='9'){if(can){t.setText(act);can=false;}else{try{if(Float.parseFloat(t.getText())==0){if(t.getText().equals("0."))t.setText(t.getText()+act);else t.setText(act);return;}else {t.setText(t.getText()+act);return;}}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);return;}}}if(act.equals("+")||act.equals("-")||act.equals("*")||act.equals("/")||act.equals("%")){if(ch!='#'){try{num1=cacu(num1,ch,Float.parseFloat(t.getText()));t.setText(String.valueOf(num1));ch=act.charAt(0);can=true;return;}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",JOptionPane.ERROR_MESSAGE);return;}}else{try{num1=Float.parseFloat(t.getText());ch=act
JAVA编写的计算器源代码
JAVA编写的计算器源代码// Calculator.java
import javax.swing.*; // 引入swing库
import java.awt.*; // 引入awt库
import java.awt.event.*; // 引入awt.event库
public class Calculator extends JFrame implements ActionListener
//定义按钮
private JButton zero;
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private JButton seven;
private JButton eight;
private JButton nine;
private JButton point;
private JButton equal; private JButton plus; private JButton minus; private JButton multiply; private JButton divide; private JButton backspace; private JButton ac;
private JButton ce;
private JButton sqrt; private JButton sqr;
Java_计算器001,支持加减乘除,括号运算
Java_计算器001,⽀持加减乘除,括号运算暑假⾃学Java期间,为了练习Java编写的计算器001版本
1import java.util.ArrayList;
2//v1.0⽀持+-*/,负号
3import java.util.Scanner;
4public class Caculator_001
5 {
6public static void main(String[] args)
7 {
8 Scanner input=new Scanner(System.in);
9double result;
10 String strIn;//="1+(2-3*(5+1)/(-4+2*(-6))-3*(+6-(-2)))+6*(5-4)";
11 System.out.println("请输⼊需要计算的式⼦:(不要有空格)");
12 strIn=input.nextLine();
13 center(strIn);
14
15 input.close();
16 }
17
18private static void center(String strIn)//计算器主体
19 {
20//计算前进⾏验证处理
21 judge(strIn);
22 String str0=strIn;//留存备份
23 strIn=minus(strIn);//负号
24 strIn=plus(strIn);//正号
25 strIn=addBrackets(strIn);//保持最外围有括号
26 ArrayList<String> jiSuan1=new ArrayList<String>();
java语言写的计算器源代码附解析
//把代码改成Calc.java编译运行,完整版计算器,最下面是计算器界面
import java.awt.*;
import java.awt.event.*;
public class Calc extends WindowAdapter implements ActionListener, MouseListener
{
Color cMoveOut=new Color(240 ,240 ,240);
Color cMoveIn =new Color( 255,255,55);
//状态变量
boolean clicked=true;//判断是否单击了小数点
boolean clear=true;//判断是否单击了符号位
double previous;//记录第一个操作数
double next;//记录第二个操作数
String fuhao;//记录符号位
int first=1;//标记是否开始一次新的运算过程
Frame f;
Panel p1,p2;
TextField tf;
Font fnt;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative,bBack;
Button bPingFang , bDaoShu , bSin, bCos ,bTan;
Button bC;
GridLayout p2Layout;
public void display()
java计算器程序带详细注释
1.import java.awt.*;
2.import java.awt.event.*;
3.import javax.swing.*;
4.
5./**比较完整的计算器
6. * @author zxsong007
7. */
8.public class Calculator extends JFrame {
9. private static final long serialVersionUID = 428884538177985224L;
10. private JButton[] buttons = new JButton[20];// 按钮
11. private JTextField textField = new JTextField("0");// 显示器
12. private char symbol;// 存放符号
13. private double tempvalues;// 临时数值
14. private double values;// 结果
15. private boolean isDone = false;// 是否计算完成,并清空显示器
16.
17. public static void main(String[] args) {// main
18. new Calculator();
19. }
20.
21. public Calculator() {// 构造
22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 点按钮关
超级简单明了的JAVA计算器代码
package caculator;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class caculator extends JFrame implements ActionListener { double sum=0;
static double getValue;
static int action ;int i=0,j=0,p=0,l;
double par1;
JButton Jk=new JButton("k");
JButton J1 = new JButton("1");
JButton J2 = new JButton("2");
JButton J3 = new JButton("3");
JButton J4 = new JButton("4");
JButton J5 = new JButton("5");
JButton J6 = new JButton("6");
JButton J7 = new JButton("7");
JButton J8 = new JButton("8");
JButton J9 = new JButton("9");
JButton J0 = new JButton("0");
JButton Je = new JButton("=");
JButton Ja = new JButton("+");
计算器设计java版的源代码
这是用java编写的计算器的源代码,界面粗糙,在elipse中运行通过,就两个类:第一个类frame ,代码如下
package jisuanqi;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class frame {
static double aa=0;//寄存器
static int bb=0;//判断是否为连续输入运算符
static int cc=0;
static char dd='d';//记录前一次运算符的值
public static void main(String args[]){
JFrame fm=new JFrame("计算器");
fm.setSize(220,180);
//fm.setMaximumSize(null);
fm.setLocation(new Point(200,100));
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container=fm.getContentPane();
GridBagLayout layout=new GridBagLayout();
java简单计算器源代码
简单计算器代码
package calcultorthree;
import java.awt.BorderLayout;//导入边界布局管理器类import java.awt.GridLayout;//导入网格布局管理器类import java.awt.TextField;// 导入文本区域类
import java.awt.event.ActionEvent;〃导入事件类
import java.awt.event.ActionListener;〃导入事件监听者类
import javax.swing.JButton;// 导入按钮类
import javax.swing.JFrame;// 导入窗体
import javax.swing.JPanel;〃导入面板
*本例实现了简单计算器代码,具备加减乘除和正弦功能,旨在抱砖引玉。熟悉java的同学,可以在此基础上实现更复杂的功能。
* @author Fjsh
*/ public class CalcultorThree {
〃新建对象,在构造函数中进行初始化
JFrame frame;//新建窗体对象
JButton buttonzero,buttondot,buttonequal;// 新建按钮“ 0”". ”"=”
JButt on butt on plus,butt onminu s,butt onm ultiple,butt on devisi on,
butto nsi n,butt on tozero;// 新建按钮“ +”-""“ * ”" / 和归零按钮JButton butt onon e,butt on two,button three,butt on four,button five,butt on six,
JAVA计算器源代码
计算器源代码
一、计算器源代码文件名:computer1.java
import java.awt.*;
import java.awt.event.*;
public class computer1 extends Frame implements ActionListener{
//声明窗口类并实现动作事件接口。Button n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;//声明数字按钮
Button op,os,om,od,oe,oc;//声明操作按钮
TextField tfd;//声明文本框
String flg,rslt;//声明标志串、结果串
Panel p1,p2,p3;//声明面板
int i1,i2;
float flt;
computer1(){
super("加减乘除计算器");
n0 = new Button("0");//实现各按钮
n1 = new Button("1");
n2 = new Button("2");
n3 = new Button("3");
n4 = new Button("4");
n5 = new Button("5");
n6 = new Button("6");
n7 = new Button("7");
n8 = new Button("8");
n9 = new Button("9");
op = new Button("加");
os = new Button("减");
om = new Button("乘");
od = new Button("除");
简单的计算器源代码
//计算器,已经编译通过
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class testZ extends JFrame implements ActionListener{
private JPanel jPanel1,jPanel2;
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2; private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;
public testZ(){
super("计算器");
setSize(300,240);
Container con=getContentPane();
con.setLayout(new BorderLayout());
jPanel1=new JPanel();
jPanel1.setLayout(new GridLayout(1,1));
jPanel2=new JPanel();
jPanel2.setLayout(new GridLayout(4,4));
resultField=new JTextField("0");
jPanel1.add(resultField);
con.add(jPanel1,BorderLayout.NORTH);
java简易计算器完整代码
java简易计算器完整代码
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculate extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
float userFloata=0f;
float userFloatb=0f;
double result;
JLabel label1 ;
JLabel label2;
JLabel label3;
JTextField textField1;
JTextField textField2;
JTextField textField3;
JButton addButton;
JButton subtractButton;
JButton rideButton;
JButton divideButton;
java简单计算器代码
java简单计算器代码
Java简单计算器代码:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num1, num2, result = 0;
char operator;
System.out.print("请输入第一个数:");
num1 = input.nextDouble();
System.out.print("请输入运算符号(+、-、*、/):"); operator = input.next().charAt(0);
System.out.print("请输入第二个数:");
num2 = input.nextDouble();
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("运算符输入错误,请重新输入!");
return;
}
System.out.println("运算结果:" + num1 + operator + num2 + "=" + result);
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//创建一个空字符串缓冲区 str=new StringBuffer();
//添加面板 p0 中的组件和设置其在框架中的位置和大小 p0.add(tf1); p0.setBounds(10,25,300,40); //添加面板 p1 中的组件和设置其在框架中的位置和大小 p1.setLayout(gl1); p1.add(tf2); p1.add(b0); p1.add(b1); p1.add(b2); p1.setBounds(10,65,300,25); //添加面板 p2 中的组件并设置其的框架中的位置和大小 p2.setLayout(gl2); p2.add(b3); p2.add(b4); p2.add(b5); p2.add(b6); p2.setBounds(10,110,40,150);
static double m;//记忆的数字 public Calculotor() { gl1=new GridLayout(1,4,10,0);//实例化三个面板的布局 gl2=new GridLayout(4,1,0,15); gl3=new GridLayout(4,5,10,15);
tf1=new JTextField(27);//显示屏 tf1.setHorizontalAlignment(JTextField.RIGHT); tf1.setEnabled(false); tf1.setText("0"); tf2=new TextField(10);//显示记忆的索引值 tf2.setEditable(false); //实例化所有按钮、设置其前景色并注册监听器 b0=new Button("Backspace"); b0.setForeground(Color.red); b0.addActionListener(new Bt()); b1=new Button("CE"); b1.setForeground(Color.red); b1.addActionListener(new Bt());
b2=new Button("C"); b2.setForeground(Color.red); b2.addActionListener(new Bt()); b3=new Button(&qwenku.baidu.comot;MC"); b3.setForeground(Color.red); b3.addActionListener(new Bt()); b4=new Button("MR"); b4.setForeground(Color.red); b4.addActionListener(new Bt()); b5=new Button("MS"); b5.setForeground(Color.red); b5.addActionListener(new Bt()); b6=new Button("M+"); b6.setForeground(Color.red); b6.addActionListener(new Bt()); b7=new Button("7"); b7.setForeground(Color.blue); b7.addActionListener(new Bt()); b8=new Button("8"); b8.setForeground(Color.blue); b8.addActionListener(new Bt()); b9=new Button("9");
ground(Color.blue);
b22.addActionListener(new Bt()); b23=new Button("+/-"); b23.setForeground(Color.blue); b23.addActionListener(new Bt()); b24=new Button("."); b24.setForeground(Color.blue); b24.addActionListener(new Bt()); b25=new Button("+"); b25.setForeground(Color.red); b25.addActionListener(new Bt()); b26=new Button("="); b26.setForeground(Color.red); b26.addActionListener(new Bt());
b16.addActionListener(new Bt()); b17=new Button("1"); b17.setForeground(Color.blue); b17.addActionListener(new Bt()); b18=new Button("2"); b18.setForeground(Color.blue); b18.addActionListener(new Bt()); b19=new Button("3"); b19.setForeground(Color.blue); b19.addActionListener(new Bt()); b20=new Button("-"); b20.setForeground(Color.red); b20.addActionListener(new Bt()); b21=new Button("1/X"); b21.setForeground(Color.blue); b21.addActionListener(new Bt()); b22=new Button("0"); b22.setFore
java 计算器源代码-有注解
import java.awt.*; import java.awt.event.*;
import javax.swing.*; public class Calculotor extends Frame { //声明三个面板的布局 GridLayout gl1,gl2,gl3; Panel p0,p1,p2,p3; JTextField tf1; TextField tf2; Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b 18,b19,b20,b21,b22,b23,b24,b25,b26; StringBuffer str;//显示屏所显示的字符串 double x,y;//x 和 y 都是运算数 int z;//Z 表 示 单 击 了 那 一 个 运 算 符 .0 表 示 "+",1 表 示 "-",2 表示"*",3 表示"/"
tf1.setText(""+(-x)); } else if(e2.getSource()==b25)//单击加号按钮获得 x 的值和 z 的值并清空 y 的值 { x=Double.parseDouble(tf1.getText().trim()); str.setLength(0);//清空缓冲区以便接收新的另一个运算数 y=0d; z=0; } else if(e2.getSource()==b20)//单击减号按钮获得 x 的值和 z 的值并清空 y 的值 { x=Double.parseDouble(tf1.getText().trim()); str.setLength(0); y=0d; z=1; } else if(e2.getSource()==b15)//单击乘号按钮获得 x 的值和 z 的值并清
p3.setBounds(60,110,250,150); //设置框架中的布局为空布局并添加 4 个面板 setLayout(null); add(p0); add(p1); add(p2); add(p3); setResizable(false);//禁止调整框架的大小 //匿名类关闭窗口 addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e1) { System.exit(0); } }); setBackground(Color.lightGray); setBounds(100,100,320,280); setVisible(true);
//添加面板 p3 中的组件并设置其在框架中的位置和大小 p3.setLayout(gl3);//设置 p3 的布局 p3.add(b7); p3.add(b8); p3.add(b9); p3.add(b10); p3.add(b11); p3.add(b12); p3.add(b13); p3.add(b14); p3.add(b15); p3.add(b16); p3.add(b17); p3.add(b18); p3.add(b19); p3.add(b20); p3.add(b21); p3.add(b22); p3.add(b23); p3.add(b24); p3.add(b25); p3.add(b26);
}
//构造监听器 class Bt implements ActionListener { public void actionPerformed(ActionEvent e2) { try{
if(e2.getSource()==b1)//选择"CE"清零 { tf1.setText("0");//把显示屏清零 str.setLength(0);//清空字符串缓冲区以准备接收新的输入运算数 } else if(e2.getSource()==b2)//选择"C"清零 { tf1.setText("0");//把显示屏清零 str.setLength(0); } else if(e2.getSource()==b23)//单击"+/-"选择输入的运算数是 正数还是负数 { x=Double.parseDouble(tf1.getText().trim());
b9.setForeground(Color.blue); b9.addActionListener(new Bt()); b10=new Button("/"); b10.setForeground(Color.red); b10.addActionListener(new Bt()); b11=new Button("sqrt"); b11.setForeground(Color.blue); b11.addActionListener(new Bt()); b12=new Button("4"); b12.setForeground(Color.blue); b12.addActionListener(new Bt()); b13=new Button("5"); b13.setForeground(Color.blue); b13.addActionListener(new Bt()); b14=new Button("6"); b14.setForeground(Color.blue); b14.addActionListener(new Bt()); b15=new Button("*"); b15.setForeground(Color.red); b15.addActionListener(new Bt()); b16=new Button("%"); b16.setForeground(Color.blue);