计算器java代码总结 基于java的计算器实现代码
Java计算器实验报告(含代码),急!!!!
给你一个吧。
在通化县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、成都网站设计 网站设计制作按需开发,公司网站建设,企业网站建设,高端网站设计,成都全网营销推广,成都外贸网站建设,通化县网站建设费用合理。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 一个计算器,与Windows附件自带计算器的标准版功能、界面相仿。
* 但还不支持键盘操作。
*/
public class Calculator extends JFrame implements ActionListener {
/** 计算器上的键的显示名字 */
private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
"*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
/** 计算器上的功能键的显示名字 */
private final String[] COMMAND = { "Backspace", "CE", "C" };
/** 计算器左边的M的显示名字 */
private final String[] M = { " ", "MC", "MR", "MS", "M+" };
/** 计算器上键的按钮 */
private JButton keys[] = new JButton[KEYS.length];
/** 计算器上的功能键的按钮 */
private JButton commands[] = new JButton[COMMAND.length];
/** 计算器左边的M的按钮 */
private JButton m[] = new JButton[M.length];
/** 计算结果文本框 */
private JTextField resultText = new JTextField("0");
// 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private boolean firstDigit = true;
// 计算的中间结果。
private double resultNum = 0.0;
// 当前运算的运算符
private String operator = "=";
// 操作是否合法
private boolean operateValidFlag = true;
/**
* 构造函数
*/
public Calculator(){
super();
//初始化计算器
init();
//设置计算器的背景颜色
this.setBackground(Color.LIGHT_GRAY);
this.setTitle("计算器");
//在屏幕(500, 300)坐标处显示计算器
this.setLocation(500, 300);
//不许修改计算器的大小
this.setResizable(false);
//使计算器中各组件大小合适
this.pack();
}
/**
* 初始化计算器
*/
private void init() {
// 文本框中的内容采用右对齐方式
resultText.setHorizontalAlignment(JTextField.RIGHT);
// 不允许修改结果文本框
resultText.setEditable(false);
// 设置文本框背景颜色为白色
resultText.setBackground(Color.white);
//初始化计算器上键的按钮,将键放在一个画板内
JPanel calckeysPanel = new JPanel();
//用网格布局器,4行,5列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3));
for (int i = 0; i KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.blue);
}
//运算符键用红色标示,其他键用蓝色表示
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
//初始化功能键,都用红色标示。将功能键放在一个画板内
JPanel commandsPanel = new JPanel();
//用网格布局器,1行,3列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].setForeground(Color.red);
}
//初始化M键,用红色标示,将M键放在一个画板内
JPanel calmsPanel = new JPanel();
//用网格布局管理器,5行,1列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
for (int i = 0; i M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].setForeground(Color.red);
}
//下面进行计算器的整体布局,将calckeys和command画板放在计算器的中部,
//将文本框放在北部,将calms画板放在计算器的西部。
//新建一个大的画板,将上面建立的command和calckeys画板放在该画板内
JPanel panel1 = new JPanel();
//画板采用边界布局管理器,画板里组件之间的水平和垂直方向上间隔都为3象素
panel1.setLayout(new BorderLayout(3, 3));
panel1.add("North", commandsPanel);
panel1.add("West", calckeysPanel);
//建立一个画板放文本框
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add("Center", resultText);
//整体布局
getContentPane().setLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
getContentPane().add("West", calmsPanel);
//为各按钮添加事件侦听器
//都使用同一个事件侦听器,即本对象。本类的声明中有implements ActionListener
for (int i = 0; i KEYS.length; i++) {
keys[i].addActionListener(this);
}
for (int i = 0; i COMMAND.length; i++) {
commands[i].addActionListener(this);
}
for (int i = 0; i M.length; i++) {
m[i].addActionListener(this);
}
}
/**
* 处理事件
*/
public void actionPerformed(ActionEvent e) {
//获取事件源的标签
String label = e.getActionCommand();
if (label.equals(COMMAND[0])){
//用户按了"Backspace"键
handleBackspace();
} else if (label.equals(COMMAND[1])) {
//用户按了"CE"键
resultText.setText("0");
} else if (label.equals(COMMAND[2])){
//用户按了"C"键
handleC();
} else if ("0123456789.".indexOf(label) = 0) {
//用户按了数字键或者小数点键
handleNumber(label);
// handlezero(zero);
} else {
//用户按了运算符键
handleOperator(label);
}
}
/**
* 处理Backspace键被按下的事件
*/
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i 0) {
//退格,将文本最后一个字符去掉
text = text.substring(0, i - 1);
if (text.length() == 0) {
//如果文本没有了内容,则初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
//显示新的文本
resultText.setText(text);
}
}
}
/**
* 处理数字键被按下的事件
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
//输入的第一个数字
resultText.setText(key);
} else if ((key.equals(".")) (resultText.getText().indexOf(".") 0)){
//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
//如果输入的不是小数点,则将数字附在结果文本框的后面
resultText.setText(resultText.getText() + key);
}
//以后输入的肯定不是第一个数字了
firstDigit = false;
}
/**
* 处理C键被按下的事件
*/
private void handleC() {
//初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
}
/**
* 处理运算符键被按下的事件
* @param key
*/
private void handleOperator(String key) {
if (operator.equals("/")) {
//除法运算
//如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
//倒数运算
if (resultNum == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("零没有倒数");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")){
//加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
//减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
//乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
//平方根运算
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")){
//百分号运算,除以100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")){
//正数负数运算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
//赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
//双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
//运算符等于用户按的按钮
operator = key;
firstDigit = true;
operateValidFlag = true;
}
/**
* 从结果文本框中获取数字
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e){
}
return result;
}
public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.setVisible(true);
calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JAVA计算器相关代码求大神{详解}
我给你找找
package com.bj.calcultor;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calcultor extends Frame implements ActionListener {
public static void main(String[] args) {//定义主方
new Calcultor();//创建匿名对象,并调用test()方法;
}
//定义按钮名称
String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};
JButton [] button=new JButton[arr.length];
JButton reset = new JButton("CE");
JTextField display = new JTextField(20);
//创建窗口,定义组件
//执行窗口事件:关闭窗口
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public Calcultor(){
super("计算器");//定义标题
//定义面板容器,并布局
JPanel jpanel=new JPanel(new GridLayout(4,4));
//添加按钮,并给按钮添加名称
for (int i = 0; i arr.length; i++) {
button[i]= new JButton(arr[i]);
jpanel.add(button[i]);
}
JPanel panel2 = new JPanel();
panel2.add("Northr", display);
display.setEnabled(false);
panel2.add("East", reset);
this.add("North", panel2);
this.add("Center", jpanel);
for (int i = 0; i arr.length; i++){
addWindowListener(new WindowCloser());
setVisible(true);
setSize(400,400);
pack();
button[i].addActionListener(this);
reset.addActionListener(this);
display.addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent e) {//定义事件
// TODO Auto-generated method stub
Object target=e.getSource();
String lable=e.getActionCommand();
if(target==reset){
handleReset();
}else if("0123456789.".indexOf(lable)0){
handleNumber(lable);
}else{
hadleOperator(lable);
}
}
boolean isFirstDigit=true;
private void hadleOperator(String key) {
if(operator.equals("+")){
number += Double.valueOf(display.getText());
}else if (operator.equals("-")){
number -= Double.valueOf(display.getText());
}else if (operator.equals("*")){
number *= Double.valueOf(display.getText());
}else if (operator.equals("/")){
number /= Double.valueOf(display.getText());
}else if(operator.equals("=")){
number =Double.valueOf(display.getText());
}
display.setText(String.valueOf(number));
operator=key;
isFirstDigit=true;
}
private void handleNumber(String key) {
if(true){
display.setText(key);//把键值设置为文本框内容
}else if(key.equals(".") display.getText().indexOf(".")0){
display.setText(display.getText()+".");//把文本框内容设置:display.getText()+"."
}else if(!key.equals(".")){
display.setText(display.getText() + key);//把文本框内容设置:display.getText()+key
isFirstDigit=false;
}
}
private void handleReset() {
display.setText("0");
isFirstDigit=true;
operator="=";
}
String operator="=";
Double number=0.0;
}
计算器java代码
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
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.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
class Normal{
double i,j;
public Normal(double num1,double num2){
i=num1;
j=num2;
}
public double puls(){
return i+j;
}
public double subtract(){
return i-j;
}
public double multiply(){
return i*j;
}
public double divide(){
return i/j;
}
public double surpuls(){
return i%j;
}
}
class scientific extends Normal{
public scientific(int num1, int num2) {
super(num1, num2);
}
}
public class calc extends JFrame{
public static void main(String[] args) {
viewNormal VN= new viewNormal("normal");
}
}
class viewNormal extends JFrame implements ActionListener{
JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));
JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));
JLabel jl;
JButton[] jb;
JButton jbs,jbo,jba,jbb,jbc,jby;
StringBuffer sb = new StringBuffer();
Normal normal;
int dot=0;
double fnum=0;
double lnum=0;
double result;
String sign=null;
public viewNormal(String title){
setTitle(title);
setLayout(null);
setVisible(true);
setBounds(200,200,305,350);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jb= new JButton[12];
for(int i=0;i9;i++){
jb[i]=new JButton(""+(i+1));
jp1.add(jb[i]);
jb[i].addActionListener(this);
}
jb[9]=new JButton(".");
jb[10]=new JButton("0");
jb[11]=new JButton("=");
jb[9].addActionListener(this);
jb[10].addActionListener(this);
jb[11].addActionListener(this);
jp1.add(jb[9]);
jp1.add(jb[10]);
jp1.add(jb[11]);
jp1.setBounds(10, 100, 200, 200);
jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");
jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");
jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);
jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);
//jp2.add(jby);
jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);
jp2.setBounds(215, 100, 70, 200);
jl= new JLabel("0",JLabel.RIGHT);
jl.setFont(new Font("Batang",Font.BOLD, 20));
jl.setBorder(new LineBorder(Color.black,2));
jl.setBackground(Color.white);
jl.setBounds(10, 40, 275, 50);
jl.setOpaque(true);
add(jl);
add(jp1);
add(jp2);
}
//+
public void sum(){
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.puls();
result=fnum;
}
//-
private void sub() {
System.out.println(sb.toString());
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.subtract();
result=fnum;
}
//*
private void mul() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.multiply();
result=fnum;
}
// /
private void div() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.divide();
result=fnum;
}
//%
private void sur() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.surpuls();
result=fnum;
}
// =
private void same(){
if(sign.equals("+")){
sum();
}
if(sign.equals("-")){
sub();
}
if(sign.equals("*")){
mul();
}
if(sign.equals("/")){
div();
}
if(sign.equals("%")){
sur();
}
}
//result
public void Result(){
if(result%1!=0)
jl.setText(""+result);
else
{
int i=(int)result;
jl.setText(""+i);
}
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(sb.toString());
// 1~9
for(int i=0;i9;i++){
if(e.getSource()==jb[i]!sb.toString().equals("0")){
sb.append(jb[i].getText());
jl.setText(sb.toString());
}
else if(e.getSource()==jb[i]sb.toString().equals("0")){
int d=sb.length();
sb.delete(0, d);
sb.append(jb[i].getText());
jl.setText(sb.toString());
}
}
// 0
if(e.getSource()==jb[10]!sb.toString().equals("0")){
sb.append(jb[10].getText());
jl.setText(sb.toString());
}
// .
if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){
dot++;
sb.append(jb[9].getText());
jl.setText(sb.toString());
}
// =
if(e.getSource()==jb[11]!sb.toString().equals("")){
same();
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// +
if(e.getSource()==jbs!sb.toString().equals("")){
if(sign!="+"sign!=null)
same();
else
sum();
sign ="+";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//-
if(e.getSource()==jbo!sb.toString().equals("")){
if(fnum==0)
fnum=2*Double.parseDouble(sb.toString());
if(sign!="-"sign!=null)
same();
else
sub();
sign ="-";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//*
if(e.getSource()==jba!sb.toString().equals("")){
if(fnum==0)
fnum=1;
if(sign!="*"sign!=null)
same();
else
mul();
sign ="*";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// /
if(e.getSource()==jbb!sb.toString().equals("")){
if(fnum==0)
fnum=Math.pow(Double.parseDouble(sb.toString()),2);
if(sign!="/"sign!=null)
same();
else
div();
sign ="/";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//%
// if(e.getSource()==jby!sb.toString().equals("")){
// if(fnum==0){
// fnum=Double.parseDouble(sb.toString());
// result=fnum;
// }
// else {
// if(sign!="%"sign!=null)
// same();
// else{
// lnum=Double.parseDouble(sb.toString());
// normal=new Normal(fnum,lnum);
// fnum=normal.surpuls();
// result=fnum;
// }
// }
// sign ="%";
// Result();
// int d=sb.length();
// sb.delete(0, d);
// dot=0;
// }
//clear
if(e.getSource()==jbc){
int d=sb.length();
sb.delete(0, d);
jl.setText("0");
dot=0;
fnum=0;
lnum=0;
sign=null;
}
}
}
class viewScientific extends viewNormal{
public viewScientific(String title){
super(title);
setBounds(200,200,800,500);
}
}
//等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。
求用JAVA实现计算器的代码(可实用的,没语法错误的)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; //导包 public class Jisuanqi extends JFrame implements ActionListener { //继承JFrame 实现事件监听 private JTextField reasult; private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel; private boolean add, sub, end, s, c; private String str; private double num1, num2; public Jisuanqi() { //构造属性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("输出"); tb.setTitleColor(Color.BLUE); //标题边框底端线 设置颜色 btnAbout = new JButton(" 关于 "); btnCancel = new JButton("Cancel"); //两个按钮 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //给Cancel添加事件监听 当鼠标点击时 程序结束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黄盖!!", "消息", JOptionPane.INFORMATION_MESSAGE); } //给“关于”添加事件监听 当鼠标点击时 弹出对话框 显示黄盖 }); p3.add(btnAbout); p3.add(btnCancel); // JPanel p4=new JPanel(); // JPanel p5=new JPanel(); // reasult.setBorder(tb); reasult = new JTextField("0", 20); reasult.setEditable(false); //设置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 设置文本的水平对齐方式。 reasult.setForeground(Color.BLUE); //颜色 p1.setBorder(tb); p1.add(reasult); btn0 = new JButton("0"); btn0.addActionListener(this); btn1 = new JButton("1"); btn1.addActionListener(this); btn2 = new JButton("2"); btn2.addActionListener(this); btn3 = new JButton("3"); btn3.addActionListener(this); btn4 = new JButton("4"); btn4.addActionListener(this); btn5 = new JButton("5"); btn5.addActionListener(this); btn6 = new JButton("6"); btn6.addActionListener(this); btn7 = new JButton("7"); btn7.addActionListener(this); btn8 = new JButton("8"); btn8.addActionListener(this); btn9 = new JButton("9"); btn9.addActionListener(this); btnD = new JButton("."); btnD.addActionListener(this); btnD.setForeground(Color.RED); btnAC = new JButton("AC"); btnAC.addActionListener(this); btnAC.setBackground(Color.PINK); btnAdd = new JButton("+"); btnAdd.addActionListener(this); btnAdd.setForeground(Color.BLUE); btnSub = new JButton("—"); btnSub.addActionListener(this); btnSub.setForeground(Color.BLUE); btnReasult = new JButton("="); btnReasult.addActionListener(this); btnReasult.setForeground(Color.RED); //事件监听 + 颜色 p2.add(btn1); p2.add(btn2); p2.add(btn3); p2.add(btn4); p2.add(btn5); p2.add(btn6); p2.add(btn7); p2.add(btn8); p2.add(btn9); p2.add(btn0); p2.add(btnD); p2.add(btnAC); p2.add(btnAdd); p2.add(btnSub); p2.add(btnReasult); //面板上添加按钮 p2.setLayout(new GridLayout(5, 3)); //面板上设置对齐方式 add(p1, BorderLayout.NORTH); add(p2, BorderLayout.CENTER); add(p3, BorderLayout.SOUTH); //将p1 p2 p3 面板对象添加到JFrame } public void num(int i) { String s = null; s = String.valueOf(i); if (end) { // 如果数字输入结束,则将文本框置零,重新输入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的内容为零,则覆盖文本框的内容 reasult.setText(s); } else { // 如果文本框的内容不为零,则在内容后面添加数字 str = reasult.getText() + s; reasult.setText(str); } }/* * * String s=null; * * s=String.valueOf(i); * * str=reasult.getText()+s; * * reasult.setText(str); * * } */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) num(1); else if (e.getSource() == btn2) num(2); else if (e.getSource() == btn3) num(3); else if (e.getSource() == btn4) num(4); else if (e.getSource() == btn5) num(5); else if (e.getSource() == btn6) num(6); else if (e.getSource() == btn7) num(7); else if (e.getSource() == btn8) num(8); else if (e.getSource() == btn9) num(9); else if (e.getSource() == btn0) num(0); else if (e.getSource() == btnAdd) { sign(1); btnD.setEnabled(true); } else if (e.getSource() == btnSub) { sign(2); btnD.setEnabled(true); } else if (e.getSource() == btnAC) { btnD.setEnabled(true); reasult.setText("0"); } else if (e.getSource() == btnD) { str = reasult.getText(); str += "."; reasult.setText(str); btnD.setEnabled(false); } else if (e.getSource() == btnReasult) { btnD.setEnabled(true); num2 = Double.parseDouble(reasult.getText()); if (add) { num1 = num1 + num2; } else if (sub) { num1 = num1 - num2; } reasult.setText(String.valueOf(num1)); end = true; } } public void sign(int s) { if (s == 1) { add = true; sub = false; } else if (s == 2) { add = false; sub = true; } num1 = Double.parseDouble(reasult.getText()); end = true; } //设计计算的过程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-简易计算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认关闭可以关闭程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 这个计算机,绝对让你满意
当前名称:计算器java代码总结 基于java的计算器实现代码
转载来源:http://ybzwz.com/article/hjdgpi.html