网站首页 > 文章中心 > 其它

java图形计算器源代码

作者:小编 更新时间:2023-09-29 14:22:18 浏览量:111人看过

java计算器的源代码

import java.awt.*;

import java.awt.event.*;

import java.lang.*;

java图形计算器源代码-图1

import javax.swing.*;

public class Counter extends Frame

{

//声明三个面板的布局

JTextField tf1;

StringBuffer str;//显示屏所显示的字符串

double x,y;//x和y都是运算数

static double m;//记忆的数字

public Counter()

tf1.setHorizontalAlignment(JTextField.RIGHT);

tf1.setEnabled(false);

java图形计算器源代码-图2

tf1.setText("0");

//实例化所有按钮、设置其前景色并注册监听器

b0=new Button("Backspace");

b0.setForeground(Color.red);

b0.addActionListener(new Bt());

b1=new Button("CE");

b1.setForeground(Color.red);

b1.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());

//实例化四个面板

p0=new Panel();

p1=new Panel();

//创建一个空字符串缓冲区

str=new StringBuffer();

//添加面板p0中的组件和设置其在框架中的位置和大小

p0.add(tf1);

//添加面板p1中的组件和设置其在框架中的位置和大小

p1.setLayout(gl1);

p1.add(b0);

p1.add(b1);

setLayout(null);

add(p0);

add(p1);

setResizable(false);//禁止调整框架的大小

//匿名类关闭窗口

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e1)

System.exit(0);

}

});

setBackground(Color.lightGray);

setVisible(true);

//构造监听器

class Bt implements ActionListener

try{

tf1.setText("0");//把显示屏清零

str.setLength(0);//清空字符串缓冲区以准备接收新的输入运算数

str.setLength(0);

x=Double.parseDouble(tf1.getText().trim());

tf1.setText(""+(-x));

str.setLength(0);//清空缓冲区以便接收新的另一个运算数

y=0d;

z=0;

z=1;

switch(z)

case 0 : tf1.setText(""+(x+y));break;

case 1 : tf1.setText(""+(x-y));break;

if(tf1.getText().trim().indexOf(+.+)!=-1)//判断字符串中是否已经包含了小数点

else//如果没数点有小

if(tf1.getText().trim().equals("0"))//如果初时显示为0

else if(tf1.getText().trim().equals(""))//如果初时显示为空则不做任何操作

else

tf1.setText("数字格式异常");

if(x0)

tf1.setText("负数没有平方根");

tf1.setText(""+Math.sqrt(x));

tf1.setText(""+(0.01*x));

if(x==0)

tf1.setText("除数不能为零");

tf1.setText(""+(1/x));

m=0d;

tf1.setText(""+m);

m=Double.parseDouble(tf1.getText().trim());

m=m+Double.parseDouble(tf1.getText().trim());

else//选择的是其他的按钮

if(tf1.getText().trim().equals("0"))//如果显示屏显示的为零不做操作

y=Double.parseDouble(tf1.getText().trim());

if(!tf1.getText().trim().equals("0"))//如果显示屏显示的不是零

if(str.length()!=1)

tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能抛出字符串越界异常

else//其他的数字键

catch(NumberFormatException e){

catch(StringIndexOutOfBoundsException e){

tf1.setText("字符串索引越界");

public static void main(String args[])

new Counter();

急:求一个简单的JAVA计算器代码,简单点,有图形界面的

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

class Stack_Float

float nums[];

int top;

Stack_Float()

top = -1;

boolean IsEmpty()

if (top == -1)

return true;

return false;

float Pop_Stack()

return 0;

top--;

return nums[top + 1];

float GetTop()

return nums[top];

void Push_Stack(float num)

return;

top++;

nums[top] = num;

class Stack_Char

char str[];

Stack_Char()

boolean CanPush(char c)

int temp = top;

if (c == '(')

while (temp != -1)

if (str[temp] == '(')

temp--;

temp = top;

if (c == '[')

if (str[temp] == '[' || str[temp] == '(')

if (c == '{')

if (str[temp] == '{' || str[temp] == '[' || str[temp] == '(')

void Push_Stack(char ch)

str[top] = ch;

char Pop_Stack()

return '\0';

return str[top + 1];

char GetTop()

System.out.print("error");

return str[top];

public class jisuanqi extends javax.swing.JFrame implements ActionListener

JTextField text = new JTextField();

JTextField text1 = new JTextField();

JButton jButton1 = new JButton();

JButton jButton10 = new JButton();

JButton jButton11 = new JButton();

String show = "";

public jisuanqi()

initComponents();

char[] TranSmit(char str[])

int i = 0, j = 0;

char c = str[i];

Stack_Char s = new Stack_Char(); // 存放运算符的栈

while (c != '=') // 对算术表达式扫描未结束时

houzhui[j] = c;

j++;

i++;

c = str[i];

houzhui[j] = '#';// 用#隔开数字

switch (c) // 扫描到运算符时

case '+':

case '-':

case '*':

case '/':

case '(':

case '[':

case '{':

if (s.IsEmpty() == true) // 栈空,直接入栈

java图形计算器源代码-图3

s.Push_Stack(c);

break;

if (ComPare(s.GetTop(), c) == -1) {

s.Push_Stack(c); // 入栈

if (ComPare(s.GetTop(), c) == 1) {

houzhui[j] = s.Pop_Stack();// 出栈元素存入后缀表达式

case ')': // 扫描到 )

while (s.GetTop() != '(') // 未扫描到 ( 时,出栈

houzhui[j] = s.Pop_Stack();

s.Pop_Stack(); // '(' 出栈

case ']': // 扫描到 ]

while (s.GetTop() != '[') // 未扫描到 [ 时,出栈

s.Pop_Stack(); // '[' 出栈

case '}': // 扫描到 }

while (s.GetTop() != '{') // 未扫描到 { 时,出栈

s.Pop_Stack(); // '{' 出栈

while (s.IsEmpty() != true)// 把剩余的运算符直接出栈

houzhui[j] = '=';// 后缀表达式后面加 =

houzhui[j] = '\0';

return houzhui;

float Count(char str[])

Stack_Float s = new Stack_Float();// 定义存放数字的栈

char c = str[0];

int i = 0;

float result = 0, temp, left, right;

while (c != '=') // 未扫描到 = 时

temp = 0;

while (c != '#')// 未读到分隔符时

temp = temp * 10 + c - '0';

s.Push_Stack(temp);// 进栈

switch (c)// 扫描到运算符时

s.Push_Stack(result);// 最后得数进栈

right = s.Pop_Stack();// 右操作数出栈

left = s.Pop_Stack();// 左操作数出栈

result = left - right;

s.Push_Stack(result);

result = left / right;

return result;

int ComPare(char a, char b) // 判断运算符的优先级函数

int s[][] = {// 栈顶元素高于算术表达式中的元素时, 返回 1,否则返回 -1

{ 1, 1, -1, -1, -1, 1, -1, 1, -1, 1 },

{ 1, 1, 1, 1, -1, 1, -1, 1, -1, 1 },

{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },

{ 1, 1, 1, 1, -1, 1, -1, -1, -1, -1 },

{ 1, 1, 1, 1, -1, -1, -1, -1, -1, 1 },

{ 1, 1, 1, 1, -1, -1, -1, -1, -1, -1 } };

char x1[] = { '+', '-', '*', '/', '(', ')', '[', ']', '{', '}' };// 栈顶元素

int k = 0, m, n = 0;

for (n = 0; n 10; n++)

k = 1;

break; // 找到比较结果后,跳出循环

if (k == 1)

return s[m][n];// 返回比较结果

public void actionPerformed(ActionEvent e)

if (e.getSource() == jButton1)

show += "1";

text.setText(show);

if (e.getSource() == jButton10)

show += "0";

if (e.getSource() == jButton11)

show += "+";

show += "-";

show += "*";

show += "/";

show += "(";

show += ")";

show += "[";

show += "]";

show += "{";

show += "}";

show = "";

text.setText("");

text1.setText("");

show += "=";

float result = 0;

str1 = show.toCharArray();

text.setText("" + result);

private void initComponents()

jButton1.setText("1");

jButton10.setText("0");

jButton11.setText("+");

jButton1.addActionListener(this);

jButton10.addActionListener(this);

jButton11.addActionListener(this);

add(text);

add(text1);

add(jButton1);

add(jButton10);

add(jButton11);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

new jisuanqi();

Java图形界面简单计算器

示例代码如下,请参考:

package?test.com.test;

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

public?class?Cal?{

StringBuffer?s1?=?new?StringBuffer();?//记录运算数字,以及保留结果

static?String?flag?=?new?String();?//标记运算符号:+,-,*,/

boolean?start?=?true;?//标记运算开始或结束,保证一次运算之后,第二次进行运算时能同时清空显示界面,即s1为空

public?void?init(){

//主界面

JFrame?f?=?new?JFrame("计算器");

JPanel?p1?=?new?JPanel();

p1.add(tf);

f.add(p1,BorderLayout.NORTH);

//动作监听器

ActionListener?listen?=new?ActionListener()?{

public?void?actionPerformed(ActionEvent?arg0)?{

if(arg0.getActionCommand()=="0"){

if(!s1.toString().equals(new?String())){

if(!start){

//s1清零,保证可以重新输入数字

s1.delete(0,?s1.length());

start?=?true;

tf.setText(s1.append("0").toString());

}?

if(arg0.getActionCommand()=="1"){

tf.setText(s1.append("1").toString());

//输入小数点

if(arg0.getActionCommand()=="."){

start=true;

if(s1.toString().equals(new?String()))

tf.setText(s1.append("0.").toString());

else?if(s1.length()==1s1.charAt(0)=='-')

tf.setText(s1.append(".").toString());

//输入负号

if(arg0.getActionCommand()=="+/-"){

tf.setText(s1.append('-').toString());

//退格Backspace

if(arg0.getActionCommand()=="Backspace"){

if(s1.length()!=0){

//删除最后输入的一位

s1.deleteCharAt(s1.length()-1);

tf.setText(s1.toString());

//归零CE

if(arg0.getActionCommand()=="CE"){

//清空所有,start标记设为true

//清除C

if(arg0.getActionCommand()=="C"){

//清空当前输入,即s1清零

//加号

if(arg0.getActionCommand()=="+"){

flag="+";

//s1清零,重新接收下一个数据

//减号

if(arg0.getActionCommand()=="-"){

flag="-";

//乘号

if(arg0.getActionCommand()=="*"){

flag="*";

//除号

if(arg0.getActionCommand()=="/"){

flag="/";

//开根号

if(arg0.getActionCommand()=="sqrt"){

start?=false;

double?s=Double.parseDouble(s1.toString());

tf.setText(s1.append(String.valueOf(Math.sqrt(s))).toString());

//求%

if(arg0.getActionCommand()=="%"){

tf.setText(s1.append(String.valueOf(s/100)).toString());

//求1/x

if(arg0.getActionCommand()=="1/x"!s1.toString().equals(new?String())Double.parseDouble(s1.toString())!=0){

tf.setText(s1.append(String.valueOf(1/s)).toString());

//求结果

//start标记为false

//四则运算

if(flag=="+"){

if(flag=="-"){

if(flag=="*"){

if(flag=="/"){

}??

};

//添加按钮以及监听器

JButton?bks?=?new?JButton("Backspace");

bks.addActionListener(listen);

JButton?ce?=?new?JButton("CE");

ce.addActionListener(listen);

JButton?c?=?new?JButton("C");

c.addActionListener(listen);

b1.addActionListener(listen);

JButton?b10?=?new?JButton("%");

b10.addActionListener(listen);

JButton?b11?=?new?JButton("1");

b11.addActionListener(listen);

f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

f.pack();

f.setVisible(true);

public?static?void?main(String?args[]){

//调用方法,实现计算器

new?Cal().init();

用JAVA编写的科学计算器源代码

class Counter extends WindowAdapter

static JFrame f=new JFrame("计算器");

static JTextField text1=new JTextField("0.");

static String source="";

static String cal="";

static String object="";

static boolean flag=false;

static boolean flag1=true;

public void init()

try

Container c=f.getContentPane();

JPanel pan1=new JPanel();

JButton b1=new JButton("1");

JButton b0=new JButton("0");

JButton b11=new JButton("+");

JButton bclar=new JButton("清零");

text1.setHorizontalAlignment(JTextField.RIGHT);

c.add(text1,"North");

c.add(pan1);

A aa=new A();

Result re=new Result();

Opertion op=new Opertion();

Clar cl=new Clar();

b1.addActionListener(aa);

b0.addActionListener(aa);

b11.addActionListener(op);

bclar.addActionListener(cl);

pan1.add(b1);

pan1.add(b11);

pan1.add(b0);

pan1.add(bclar);

catch(Exception e)

System.out.println(e.getMessage());

class A implements ActionListener

String a=text1.getText();

String s=e.getActionCommand();

if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))

text1.setText(s);

else {

text1.setText(a+s);

class Opertion implements ActionListener

cal=e.getActionCommand();

if(flag1==true)

source=text1.getText();

text1.setText(cal);

flag1=false;

flag=true;

class Result implements ActionListener

double num1;

num1=Double.parseDouble(source);

object=text1.getText();

double result=0;

if(cal.equals("+"))

if(cal.equals("-"))

if(cal.equals("*"))

if(cal.equals("/"))

text1.setText("除数不能为0");

String s1=Double.toString(result);

text1.setText(s1);

flag1=true;

class Clar implements ActionListener

text1.setText("0.");

public static void main(String[] args)

Counter count=new Counter();

count.init();

public void windowClosing(WindowEvent e){

System.exit(1);

public void windowOpened(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

Java程序--编写一个具有图形界面的计算器程序

import java.io.*;

public class Test implements ActionListener

Frame f = new Frame("一步运算计算器");//创建窗架

Label La1 = new Label("+");

Button btn = new Button("=");

public Test()

f.setLayout(new FlowLayout());

f.addWindowListener

(

new WindowAdapter()

public void windowClosing(WindowEvent e)

); //窗口关闭事件

btn.addActionListener //输出加法运算结果

new ActionListener()

);

tf[i].addActionListener(this); //创建文本域,注册事件监听器

tf[0].requestFocusInWindow(); //光标定位

f.add(tf[0]);

f.add(La1);

f.add(tf[1]);

f.add(btn);

f.show();

if(e.getSource()==tf[0])

a[0]=Integer.valueOf(tf[0].getText()).intValue();

tf[1].requestFocusInWindow();

if(e.getSource()==tf[1])

a[1]=Integer.valueOf(tf[1].getText()).intValue();

btn.requestFocusInWindow();

catch(Exception ex)

public static void main(String[] args) throws Exception

Test t = new Test();

在文本域中输入完后必须按Enter才能完成正确的功能.再按等号按钮输出结果.

JAVA计算器代码

import java.applet.*;

public class CalculatorDemo extends Applet

TextField answerText;

Button pointButton,equalButton,plusButton;

Button[] b=new Button[10];

String currentOp,preOp;

String foreText,backText;

boolean isFloat=false;

Panel p1=new Panel();

currentOp=new String("");

preOp=new String("");

foreText=new String("");

backText=new String("");

setForeground(Color.blue);

b[i]=new Button(Integer.toString(i));

pointButton=new Button(".");

equalButton=new Button("=");

equalButton.setForeground(Color.red);

clearButton=new Button("清除");

clearButton.setForeground(Color.red);

multiButton=new Button("*");

multiButton.setForeground(Color.red);

minusButton=new Button("-");

minusButton.setForeground(Color.red);

plusButton=new Button("+");

plusButton.setForeground(Color.red);

setLayout(new FlowLayout());

p1.setLayout(new FlowLayout());

p1.add(answerText);

p1.add(clearButton);

public boolean action(Event e,Object o)

String s=new String("");

for(int i=0;i10;i++)

if(e.target==b[i]||e.target==pointButton)

if(e.target !=pointButton)

s=(String)o;

doForeText(s);

if((e.target==pointButton)(!isFloat))

isFloat=true;

if(foreText.equals(""))

foreText +="0.";

if(e.target==clearButton)

doClear();

||(e.target==plusButton)||(e.target==minusButton))

if(foreText !="")

currentOp=((String)o);

doOperator();

preOp=((String)o);

if(e.target==equalButton)

public void doOperator()

double dFore,dBack;

Double d;

if(preOp.equals(""))

backText=foreText;

foreText="";

answerText.setText(backText);

dFore=(new Double(foreText)).doubleValue();

dBack=(new Double(backText)).doubleValue();

backText=answerText.getText();

if(preOp.equals("+"))

d=new Double((dBack+dFore));

answerText.setText(d.toString());

backText=d.toString();

if(preOp.equals("-"))

d=new Double((dBack-dFore));

if(preOp.equals("*"))

d=new Double((dBack*dFore));

if(preOp.equals("/"))

if(dFore==0)

answerText.setText("除数不能为0");

d=new Double((dBack/dFore));

preOp=currentOp;

public void doForeText(String s)

foreText +=s;

answerText.setText(foreText);

public void doBackText(String s)

public void doClear()

currentOp="";

preOp="";

backText="";

isFloat=false;

answerText.setText("");

以上就是土嘎嘎小编为大家整理的java图形计算器源代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章