java图形计算器源代码,java图形计算器源代码怎么用

java计算器的源代码

import java.awt.*;

创新互联于2013年创立,先为解放等服务建站,解放等地企业,进行企业商务咨询服务。为解放企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

import java.awt.event.*;

import java.lang.*;

import javax.swing.*;

public class Counter 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,b18,b19,b20,b21,b22,b23,b24,b25,b26;

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

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

int z;//Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"

static double m;//记忆的数字

public Counter()

{

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("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");

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);

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

//实例化四个面板

p0=new Panel();

p1=new Panel();

p2=new Panel();

p3=new Panel();

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

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);

//添加面板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);

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);

}

//构造监听器

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

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的值并清空y的值

{

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

str.setLength(0);

y=0d;

z=2;

}

else if(e2.getSource()==b10)//单击除号按钮获得x的值和z的值并空y的值

{

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

str.setLength(0);

y=0d;

z=3;

}

else if(e2.getSource()==b26)//单击等号按钮输出计算结果

{

str.setLength(0);

switch(z)

{

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

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

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

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

}

}

else if(e2.getSource()==b24)//单击"."按钮输入小数

{

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

{

}

else//如果没数点有小

{

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

{

str.setLength(0);

tf1.setText((str.append("0"+e2.getActionCommand())).toString());

}

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

{

}

else

{

tf1.setText(str.append(e2.getActionCommand()).toString());

}

}

y=0d;

}

else if(e2.getSource()==b11)//求平方根

{

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

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

if(x0)

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

else

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

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b16)//单击了"%"按钮

{

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

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

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b21)//单击了"1/X"按钮

{

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

if(x==0)

{

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

}

else

{

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

}

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b3)//MC为清除内存

{

m=0d;

tf2.setText("");

str.setLength(0);

}

else if(e2.getSource()==b4)//MR为重新调用存储的数据

{

if(tf2.getText().trim()!="")//有记忆数字

{

tf1.setText(""+m);

}

}

else if(e2.getSource()==b5)//MS为存储显示的数据

{

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

tf2.setText("M");

tf1.setText("0");

str.setLength(0);

}

else if(e2.getSource()==b6)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR

{

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

}

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

{

if(e2.getSource()==b22)//如果选择的是"0"这个数字键

{

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

{

}

else

{

tf1.setText(str.append(e2.getActionCommand()).toString());

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

}

}

else if(e2.getSource()==b0)//选择的是“BackSpace”按钮

{

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

{

if(str.length()!=1)

{

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

}

else

{

tf1.setText("0");

str.setLength(0);

}

}

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

}

else//其他的数字键

{

tf1.setText(str.append(e2.getActionCommand()).toString());

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

}

}

}

catch(NumberFormatException e){

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

}

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()

{

nums = new float[50];

top = -1;

}

boolean IsEmpty()

{

if (top == -1)

return true;

else

return false;

}

float Pop_Stack()

{

if (top == -1)

{

return 0;

}

top--;

return nums[top + 1];

}

float GetTop()

{

return nums[top];

}

void Push_Stack(float num)

{

if (top == 49)

return;

top++;

nums[top] = num;

}

}

class Stack_Char

{

char str[];

int top;

Stack_Char()

{

str = new char[50];

top = -1;

}

boolean CanPush(char c)

{

int temp = top;

if (c == '(')

{

while (temp != -1)

{

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

{

return false;

}

temp--;

}

}

temp = top;

if (c == '[')

{

while (temp != -1)

{

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

{

return false;

}

temp--;

}

}

if (c == '{')

{

while (temp != -1)

{

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

{

return false;

}

temp--;

}

}

return true;

}

boolean IsEmpty()

{

if (top == -1)

return true;

else

return false;

}

void Push_Stack(char ch)

{

if (top == 49)

return;

top++;

str[top] = ch;

}

char Pop_Stack()

{

if (top == -1)

return '\0';

top--;

return str[top + 1];

}

char GetTop()

{

if (top == -1)

{

System.out.print("error");

System.exit(0);

}

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 jButton2 = new JButton();

JButton jButton3 = new JButton();

JButton jButton4 = new JButton();

JButton jButton5 = new JButton();

JButton jButton6 = new JButton();

JButton jButton7 = new JButton();

JButton jButton8 = new JButton();

JButton jButton9 = new JButton();

JButton jButton10 = new JButton();

JButton jButton11 = new JButton();

JButton jButton12 = new JButton();

JButton jButton13 = new JButton();

JButton jButton14 = new JButton();

JButton jButton15 = new JButton();

JButton jButton16 = new JButton();

JButton jButton17 = new JButton();

JButton jButton18 = new JButton();

JButton jButton19 = new JButton();

JButton jButton20 = new JButton();

JButton jButton21 = new JButton();

JButton jButton22 = new JButton();

String show = "";

public jisuanqi()

{

initComponents();

}

char[] TranSmit(char str[])

{

char houzhui[] = new char[50]; // 存放后缀表达式的字符串

int i = 0, j = 0;

char c = str[i];

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

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

{

if (c = '0' c = '9')

{

while (c = '0' c = '9')// 数字直接入栈

{

houzhui[j] = c;

j++;

i++;

c = str[i];

}

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

j++;

}

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

{

case '+':

case '-':

case '*':

case '/':

case '(':

case '[':

case '{':

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

{

s.Push_Stack(c);

i++;

c = str[i];

break;

}

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

s.Push_Stack(c); // 入栈

i++;

c = str[i];

break;

}

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

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

j++;

break;

}

case ')': // 扫描到 )

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

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

i++;

c = str[i];

break;

case ']': // 扫描到 ]

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

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

i++;

c = str[i];

break;

case '}': // 扫描到 }

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

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

i++;

c = str[i];

break;

}

}

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

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

j++;

houzhui[j] = '\0';

j++;

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 != '=') // 未扫描到 = 时

{

if (c = '0' c = '9')// 扫描到数字

{

temp = 0;

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

{

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

i++;

c = str[i];

}

s.Push_Stack(temp);// 进栈

}

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

{

case '+':

{

result = s.Pop_Stack() + s.Pop_Stack();// 2个数字出栈相加

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

break;

}

case '-':

{

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

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

result = left - right;

s.Push_Stack(result);

break;

}

case '*':

{

result = s.Pop_Stack() * s.Pop_Stack();// 2个数字出栈相乘

s.Push_Stack(result);

break;

}

case '/':

{

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

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

result = left / right;

s.Push_Stack(result);

break;

}

}

i++;

c = str[i];

}

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 },

{ -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[] = { '+', '-', '*', '/', '(', ')', '[', ']', '{', '}' };// 栈顶元素

char x2[] = { '+', '-', '*', '/', '(', ')', '[', ']', '{', '}' };// 算术表达式中的元素

int k = 0, m, n = 0;

for (m = 0; m 10; m++) // 查找2个进行比较的运算符在表中的位置,并返回比较结果

{

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

{

if (x1[m] == a x2[n] == b)

{

k = 1;

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

}

}

if (k == 1)

break;

}

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

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == jButton1)

{

show += "1";

text.setText(show);

}

if (e.getSource() == jButton2)

{

show += "2";

text.setText(show);

}

if (e.getSource() == jButton3)

{

show += "3";

text.setText(show);

}

if (e.getSource() == jButton4)

{

show += "4";

text.setText(show);

}

if (e.getSource() == jButton5)

{

show += "5";

text.setText(show);

}

if (e.getSource() == jButton6)

{

show += "6";

text.setText(show);

}

if (e.getSource() == jButton7)

{

show += "7";

text.setText(show);

}

if (e.getSource() == jButton8)

{

show += "8";

text.setText(show);

}

if (e.getSource() == jButton9)

{

show += "9";

text.setText(show);

}

if (e.getSource() == jButton10)

{

show += "0";

text.setText(show);

}

if (e.getSource() == jButton11)

{

show += "+";

text.setText(show);

}

if (e.getSource() == jButton12)

{

show += "-";

text.setText(show);

}

if (e.getSource() == jButton13)

{

show += "*";

text.setText(show);

}

if (e.getSource() == jButton14)

{

show += "/";

text.setText(show);

}

if (e.getSource() == jButton15)

{

show += "(";

text.setText(show);

}

if (e.getSource() == jButton16)

{

show += ")";

text.setText(show);

}

if (e.getSource() == jButton17)

{

show += "[";

text.setText(show);

}

if (e.getSource() == jButton18)

{

show += "]";

text.setText(show);

}

if (e.getSource() == jButton19)

{

show += "{";

text.setText(show);

}

if (e.getSource() == jButton20)

{

show += "}";

text.setText(show);

}

if (e.getSource() == jButton21)

{

show = "";

text.setText("");

text1.setText("");

}

if (e.getSource() == jButton22)

{

show += "=";

text.setText(show);

char str1[] = new char[50];

char str2[] = new char[50];

float result = 0;

str1 = show.toCharArray();

str2 = TranSmit(str1);

result = Count(str2);

text1.setText((new String(str2)).trim());

text.setText("" + result);

show = "";

}

}

private void initComponents()

{

text.setBounds(10, 10, 270, 30);

text1.setBounds(10, 50, 270, 30);

jButton1.setBounds(10, 90, 60, 25);

jButton2.setBounds(80, 90, 60, 25);

jButton3.setBounds(150, 90, 60, 25);

jButton4.setBounds(220, 90, 60, 25);

jButton5.setBounds(10, 120, 60, 25);

jButton6.setBounds(80, 120, 60, 25);

jButton7.setBounds(150, 120, 60, 25);

jButton8.setBounds(220, 120, 60, 25);

jButton9.setBounds(10, 150, 60, 25);

jButton10.setBounds(80, 150, 60, 25);

jButton11.setBounds(150, 150, 60, 25);

jButton12.setBounds(220, 150, 60, 25);

jButton13.setBounds(10, 180, 60, 25);

jButton14.setBounds(80, 180, 60, 25);

jButton15.setBounds(150, 180, 60, 25);

jButton16.setBounds(220, 180, 60, 25);

jButton17.setBounds(150, 210, 60, 25);

jButton18.setBounds(220, 210, 60, 25);

jButton19.setBounds(10, 210, 60, 25);

jButton20.setBounds(80, 210, 60, 25);

jButton21.setBounds(10, 240, 60, 25);

jButton22.setBounds(80, 240, 60, 25);

jButton1.setText("1");

jButton2.setText("2");

jButton3.setText("3");

jButton4.setText("4");

jButton5.setText("5");

jButton6.setText("6");

jButton7.setText("7");

jButton8.setText("8");

jButton9.setText("9");

jButton10.setText("0");

jButton11.setText("+");

jButton12.setText("-");

jButton13.setText("*");

jButton14.setText("/");

jButton15.setText("(");

jButton16.setText(")");

jButton17.setText("[");

jButton18.setText("]");

jButton19.setText("{");

jButton20.setText("}");

jButton21.setText("CE");

jButton22.setText("=");

jButton1.addActionListener(this);

jButton2.addActionListener(this);

jButton3.addActionListener(this);

jButton4.addActionListener(this);

jButton5.addActionListener(this);

jButton6.addActionListener(this);

jButton7.addActionListener(this);

jButton8.addActionListener(this);

jButton9.addActionListener(this);

jButton10.addActionListener(this);

jButton11.addActionListener(this);

jButton12.addActionListener(this);

jButton13.addActionListener(this);

jButton14.addActionListener(this);

jButton15.addActionListener(this);

jButton16.addActionListener(this);

jButton17.addActionListener(this);

jButton18.addActionListener(this);

jButton19.addActionListener(this);

jButton20.addActionListener(this);

jButton21.addActionListener(this);

jButton22.addActionListener(this);

add(text);

add(text1);

add(jButton1);

add(jButton2);

add(jButton3);

add(jButton4);

add(jButton5);

add(jButton6);

add(jButton7);

add(jButton8);

add(jButton9);

add(jButton10);

add(jButton11);

add(jButton12);

add(jButton13);

add(jButton14);

add(jButton15);

add(jButton16);

add(jButton17);

add(jButton18);

add(jButton19);

add(jButton20);

add(jButton21);

add(jButton22);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);

setBounds(300, 300, 300, 300);

setVisible(true);

}

public static void main(String args[])

{

new jisuanqi();

}

}

Java图形界面简单计算器

示例代码如下,请参考:

package test.com.test;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Cal {

TextField tf = new TextField(40);       //显示文本框

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

StringBuffer s2 = 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);

JPanel p2 = new JPanel();

p2.setLayout(new GridLayout(1,3));

//动作监听器

ActionListener listen =new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

//输入数字0~9

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"){

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

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

if(!start){

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

}

start = true;

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

}

//输入小数点

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

if(!start){

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

}

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("0.").toString());

else

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

}

//输入负号

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

if(!start){

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

}

start = true;

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

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

}

//退格Backspace

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

start = true;

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

//删除最后输入的一位

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

tf.setText(s1.toString());

}

}

//归零CE

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

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

start = true;

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

s2.delete(0, s2.length());

tf.setText(s1.toString());

}

//清除C

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

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

start = true;

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

tf.setText(s1.toString());

}

//加号

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

start = true;

flag="+";

//s2接收s1的值

s2.append(String.valueOf(s1.toString()));

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

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

tf.setText(s1.toString());

}

//减号

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

start = true;

flag="-";

s2.append(String.valueOf(s1.toString()));

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

tf.setText(s1.toString());

}

//乘号

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

start = true;

flag="*";

s2.append(String.valueOf(s1.toString()));

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

tf.setText(s1.toString());

}

//除号

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

start = true;

flag="/";

s2.append(String.valueOf(s1.toString()));

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

tf.setText(s1.toString());

}

//开根号

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

start =false;

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

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

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

}

//求%

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

start =false;

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

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

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){

start =false;

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

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

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

}

//求结果

if(arg0.getActionCommand()=="="s1.length()!=0s2.length()!=0){

//start标记为false

start =false;

//分别获取s1和s2的值

double s4=Double.parseDouble(s1.toString()); 

double s3=Double.parseDouble(s2.toString());

//清空s1和s2,便于开始下一次运算

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

s2.delete(0, s2.length());

//四则运算

if(flag=="+"){

tf.setText(s1.append(String.valueOf(s3+s4)).toString());

}

if(flag=="-"){

tf.setText(s1.append(String.valueOf(s3-s4)).toString());

}

if(flag=="*"){

tf.setText(s1.append(String.valueOf(s3*s4)).toString());

}

if(flag=="/"){

tf.setText(s1.append(String.valueOf(s3/s4)).toString());

}

}       

}

};

//添加按钮以及监听器

JButton bks = new JButton("Backspace");

bks.addActionListener(listen);

JButton ce = new JButton("CE");

ce.addActionListener(listen);

JButton c = new JButton("C");

c.addActionListener(listen);

p2.add(bks);

p2.add(ce);

p2.add(c);

f.add(p2);

JPanel p3 = new JPanel();

p3.setLayout(new GridLayout(4,5));

JButton b1 = new JButton("7");

p3.add(b1);

b1.addActionListener(listen);

JButton b2 = new JButton("8");

p3.add(b2);

b2.addActionListener(listen);

JButton b3 = new JButton("9");

p3.add(b3);

b3.addActionListener(listen);

JButton b4 = new JButton("/");

p3.add(b4);

b4.addActionListener(listen);

JButton b5 = new JButton("sqrt");

p3.add(b5);

b5.addActionListener(listen);

JButton b6 = new JButton("4");

p3.add(b6);

b6.addActionListener(listen);

JButton b7 = new JButton("5");

p3.add(b7);

b7.addActionListener(listen);

JButton b8 = new JButton("6");

p3.add(b8);

b8.addActionListener(listen);

JButton b9 = new JButton("*");

p3.add(b9);

b9.addActionListener(listen);

JButton b10 = new JButton("%");

p3.add(b10);

b10.addActionListener(listen);

JButton b11 = new JButton("1");

p3.add(b11);

b11.addActionListener(listen);

JButton b12 = new JButton("2");

p3.add(b12);

b12.addActionListener(listen);

JButton b13 = new JButton("3");

p3.add(b13);

b13.addActionListener(listen);

JButton b14 = new JButton("-");

p3.add(b14);

b14.addActionListener(listen);

JButton b15 = new JButton("1/x");

p3.add(b15);

b15.addActionListener(listen);

JButton b16 = new JButton("0");

p3.add(b16);

b16.addActionListener(listen);

JButton b17 = new JButton("+/-");

p3.add(b17);

b17.addActionListener(listen);

JButton b18 = new JButton(".");

p3.add(b18);

b18.addActionListener(listen);

JButton b19 = new JButton("+");

p3.add(b19);

b19.addActionListener(listen);

JButton b20 = new JButton("=");

p3.add(b20);

b20.addActionListener(listen);

f.add(p3,BorderLayout.SOUTH);

f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

f.pack();

f.setVisible(true);

}

public static void main(String args[]){

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

new Cal().init();

}

}

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

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

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;

static boolean flag2=false;

public void init()

{

try

{

Container c=f.getContentPane();

JPanel pan1=new JPanel();

JButton b1=new JButton("1");

JButton b2=new JButton("2");

JButton b3=new JButton("3");

JButton b4=new JButton("4");

JButton b5=new JButton("5");

JButton b6=new JButton("6");

JButton b7=new JButton("7");

JButton b8=new JButton("8");

JButton b9=new JButton("9");

JButton b0=new JButton("0");

JButton b11=new JButton("+");

JButton b12=new JButton("-");

JButton b13=new JButton("*");

JButton b14=new JButton("/");

JButton b15=new JButton(".");

JButton b16=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);

b2.addActionListener(aa);

b3.addActionListener(aa);

b4.addActionListener(aa);

b5.addActionListener(aa);

b6.addActionListener(aa);

b7.addActionListener(aa);

b8.addActionListener(aa);

b9.addActionListener(aa);

b0.addActionListener(aa);

b11.addActionListener(op);

b12.addActionListener(op);

b13.addActionListener(op);

b14.addActionListener(op);

b16.addActionListener(re);

b15.addActionListener(aa);

bclar.addActionListener(cl);

pan1.add(b1);

pan1.add(b2);

pan1.add(b3);

pan1.add(b11);

pan1.add(b4);

pan1.add(b5);

pan1.add(b6);

pan1.add(b12);

pan1.add(b7);

pan1.add(b8);

pan1.add(b9);

pan1.add(b13);

pan1.add(b0);

pan1.add(b15);

pan1.add(b16);

pan1.add(b14);

pan1.add(bclar);

f.setSize(200,220);

f.setVisible(true);

}

catch(Exception e)

{

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

}

}

class A implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String a=text1.getText();

String s=e.getActionCommand();

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

text1.setText(s);

else {

if(flag2)

{

text1.setText(s);

flag2=false;

}

else

text1.setText(a+s);

}

}

}

class Opertion implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

cal=e.getActionCommand();

if(flag1==true)

source=text1.getText();

text1.setText(cal);

flag1=false;

flag=true;

}

}

class Result implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

double num1;

num1=Double.parseDouble(source);

object=text1.getText();

double num2;

num2=Double.parseDouble(object);

double result=0;

if(cal.equals("+"))

result=num1+num2;

if(cal.equals("-"))

result=num1-num2;

if(cal.equals("*"))

result=num1*num2;

if(cal.equals("/"))

if(num2==0)

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

else

result=num1/num2;

String s1=Double.toString(result);

text1.setText(s1);

flag1=true;

flag2=true;

}

}

class Clar implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

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.awt.*;

import java.awt.event.*;

import java.io.*;

public class Test implements ActionListener

{

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

TextField[] tf = new TextField[3];

Label La1 = new Label("+");

Button btn = new Button("=");

int[] a = new int[3]; //储存数据

public Test()

{

f.setLayout(new FlowLayout());

f.setSize(400,100);

f.addWindowListener

(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

); //窗口关闭事件

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

(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

tf[2].setText(Integer.toString(a[2]));

}

}

);

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

{

tf[i] = new TextField(8);

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

}

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

f.add(tf[0]);

f.add(La1);

f.add(tf[1]);

f.add(btn);

f.add(tf[2]);

f.show();

}

public void actionPerformed(ActionEvent e)

{

try

{

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

}

a[2] = a[0]+a[1];

}

catch(Exception ex)

{

}

}

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

{

Test t = new Test();

}

}

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

JAVA计算器代码

import java.awt.*;

import java.applet.*;

public class CalculatorDemo extends Applet

{

TextField answerText;

Button pointButton,equalButton,plusButton;

Button minusButton,clearButton,multiButton,divisionButton;

Button[] b=new Button[10];

String currentOp,preOp;

String foreText,backText;

boolean isFloat=false;

public void init()

{

Panel p1=new Panel();

Panel p2=new Panel();

Panel p3=new Panel();

currentOp=new String("");

preOp=new String("");

foreText=new String("");

backText=new String("");

answerText=new TextField(8);

setBackground(Color.lightGray);

setForeground(Color.blue);

for(int i=9;i=0;i--)

{

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

p2.add(b[i]);

}

pointButton=new Button(".");

equalButton=new Button("=");

equalButton.setForeground(Color.red);

clearButton=new Button("清除");

clearButton.setForeground(Color.red);

divisionButton=new Button("/");

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

p2.setLayout(new GridLayout(4,3));

p3.setLayout(new GridLayout(4,1));

p1.add(answerText);

p1.add(clearButton);

p2.add(pointButton);

p2.add(equalButton);

p3.add(plusButton);

p3.add(minusButton);

p3.add(multiButton);

p3.add(divisionButton);

add(p1);

add(p2);

add(p3);

}

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;

s=(String)o;

if(foreText.equals(""))

{

foreText +="0.";

}

else

{

doForeText(s);

}

}

}

}

if(e.target==clearButton)

{

doClear();

}

if((e.target==clearButton)||(e.target==divisionButton)

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

{

if(foreText !="")

{

currentOp=((String)o);

doOperator();

}

else

{

preOp=((String)o);

}

}

if(e.target==equalButton)

{

doOperator();

}

return true;

}

public void doOperator()

{

double dFore,dBack;

Double d;

if(preOp.equals(""))

{

backText=foreText;

foreText="";

answerText.setText(backText);

}

else

{

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

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

foreText="";

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));

answerText.setText(d.toString());

backText=d.toString();

}

if(preOp.equals("*"))

{

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

answerText.setText(d.toString());

backText=d.toString();

}

if(preOp.equals("/"))

{

if(dFore==0)

{

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

return;

}

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

answerText.setText(d.toString());

backText=d.toString();

}

}

preOp=currentOp;

}

public void doForeText(String s)

{

foreText +=s;

answerText.setText(foreText);

}

public void doBackText(String s)

{

backText=foreText;

foreText="";

answerText.setText(foreText);

}

public void doClear()

{

currentOp="";

preOp="";

foreText="";

backText="";

isFloat=false;

answerText.setText("");

}

}


新闻名称:java图形计算器源代码,java图形计算器源代码怎么用
网站网址:http://pwwzsj.com/article/hsjgid.html