最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
java圖形計(jì)算器源代碼,java圖形計(jì)算器源代碼怎么用

java計(jì)算器的源代碼

import java.awt.*;

創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為解放等服務(wù)建站,解放等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為解放企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

import java.awt.event.*;

import java.lang.*;

import javax.swing.*;

public class Counter extends Frame

{

//聲明三個(gè)面板的布局

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都是運(yùn)算數(shù)

int z;//Z表示單擊了那一個(gè)運(yùn)算符.0表示"+",1表示"-",2表示"*",3表示"/"

static double m;//記憶的數(shù)字

public Counter()

{

gl1=new GridLayout(1,4,10,0);//實(shí)例化三個(gè)面板的布局

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

//實(shí)例化所有按鈕、設(shè)置其前景色并注冊(cè)監(jiān)聽(tīng)器

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

//實(shí)例化四個(gè)面板

p0=new Panel();

p1=new Panel();

p2=new Panel();

p3=new Panel();

//創(chuàng)建一個(gè)空字符串緩沖區(qū)

str=new StringBuffer();

//添加面板p0中的組件和設(shè)置其在框架中的位置和大小

p0.add(tf1);

p0.setBounds(10,25,300,40);

//添加面板p1中的組件和設(shè)置其在框架中的位置和大小

p1.setLayout(gl1);

p1.add(tf2);

p1.add(b0);

p1.add(b1);

p1.add(b2);

p1.setBounds(10,65,300,25);

//添加面板p2中的組件并設(shè)置其的框架中的位置和大小

p2.setLayout(gl2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.setBounds(10,110,40,150);

//添加面板p3中的組件并設(shè)置其在框架中的位置和大小

p3.setLayout(gl3);//設(shè)置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);

//設(shè)置框架中的布局為空布局并添加4個(gè)面板

setLayout(null);

add(p0);

add(p1);

add(p2);

add(p3);

setResizable(false);//禁止調(diào)整框架的大小

//匿名類關(guān)閉窗口

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e1)

{

System.exit(0);

}

});

setBackground(Color.lightGray);

setBounds(100,100,320,280);

setVisible(true);

}

//構(gòu)造監(jiān)聽(tīng)器

class Bt implements ActionListener

{

public void actionPerformed(ActionEvent e2)

{

try{

if(e2.getSource()==b1)//選擇"CE"清零

{

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

str.setLength(0);//清空字符串緩沖區(qū)以準(zhǔn)備接收新的輸入運(yùn)算數(shù)

}

else if(e2.getSource()==b2)//選擇"C"清零

{

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

str.setLength(0);

}

else if(e2.getSource()==b23)//單擊"+/-"選擇輸入的運(yùn)算數(shù)是正數(shù)還是負(fù)數(shù)

{

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

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

}

else if(e2.getSource()==b25)//單擊加號(hào)按鈕獲得x的值和z的值并清空y的值

{

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

str.setLength(0);//清空緩沖區(qū)以便接收新的另一個(gè)運(yùn)算數(shù)

y=0d;

z=0;

}

else if(e2.getSource()==b20)//單擊減號(hào)按鈕獲得x的值和z的值并清空y的值

{

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

str.setLength(0);

y=0d;

z=1;

}

else if(e2.getSource()==b15)//單擊乘號(hào)按鈕獲得x的值和z的值并清空y的值

{

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

str.setLength(0);

y=0d;

z=2;

}

else if(e2.getSource()==b10)//單擊除號(hào)按鈕獲得x的值和z的值并空y的值

{

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

str.setLength(0);

y=0d;

z=3;

}

else if(e2.getSource()==b26)//單擊等號(hào)按鈕輸出計(jì)算結(jié)果

{

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)//單擊"."按鈕輸入小數(shù)

{

if(tf1.getText().trim().indexOf(′.′)!=-1)//判斷字符串中是否已經(jīng)包含了小數(shù)點(diǎn)

{

}

else//如果沒(méi)數(shù)點(diǎn)有小

{

if(tf1.getText().trim().equals("0"))//如果初時(shí)顯示為0

{

str.setLength(0);

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

}

else if(tf1.getText().trim().equals(""))//如果初時(shí)顯示為空則不做任何操作

{

}

else

{

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

}

}

y=0d;

}

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

{

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

tf1.setText("數(shù)字格式異常");

if(x0)

tf1.setText("負(fù)數(shù)沒(méi)有平方根");

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("除數(shù)不能為零");

}

else

{

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

}

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b3)//MC為清除內(nèi)存

{

m=0d;

tf2.setText("");

str.setLength(0);

}

else if(e2.getSource()==b4)//MR為重新調(diào)用存儲(chǔ)的數(shù)據(jù)

{

if(tf2.getText().trim()!="")//有記憶數(shù)字

{

tf1.setText(""+m);

}

}

else if(e2.getSource()==b5)//MS為存儲(chǔ)顯示的數(shù)據(jù)

{

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

tf2.setText("M");

tf1.setText("0");

str.setLength(0);

}

else if(e2.getSource()==b6)//M+為將顯示的數(shù)字與已經(jīng)存儲(chǔ)的數(shù)據(jù)相加要查看新的數(shù)字單擊MR

{

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

}

else//選擇的是其他的按鈕

{

if(e2.getSource()==b22)//如果選擇的是"0"這個(gè)數(shù)字鍵

{

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//其他的數(shù)字鍵

{

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

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

}

}

}

catch(NumberFormatException e){

tf1.setText("數(shù)字格式異常");

}

catch(StringIndexOutOfBoundsException e){

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

}

}

}

public static void main(String args[])

{

new Counter();

急:求一個(gè)簡(jiǎn)單的JAVA計(jì)算器代碼,簡(jiǎn)單點(diǎn),有圖形界面的

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]; // 存放后綴表達(dá)式的字符串

int i = 0, j = 0;

char c = str[i];

Stack_Char s = new Stack_Char(); // 存放運(yùn)算符的棧

while (c != '=') // 對(duì)算術(shù)表達(dá)式掃描未結(jié)束時(shí)

{

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

{

while (c = '0' c = '9')// 數(shù)字直接入棧

{

houzhui[j] = c;

j++;

i++;

c = str[i];

}

houzhui[j] = '#';// 用#隔開(kāi)數(shù)字

j++;

}

switch (c) // 掃描到運(yùn)算符時(shí)

{

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();// 出棧元素存入后綴表達(dá)式

j++;

break;

}

case ')': // 掃描到 )

while (s.GetTop() != '(') // 未掃描到 ( 時(shí),出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

i++;

c = str[i];

break;

case ']': // 掃描到 ]

while (s.GetTop() != '[') // 未掃描到 [ 時(shí),出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

i++;

c = str[i];

break;

case '}': // 掃描到 }

while (s.GetTop() != '{') // 未掃描到 { 時(shí),出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

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

i++;

c = str[i];

break;

}

}

while (s.IsEmpty() != true)// 把剩余的運(yùn)算符直接出棧

{

houzhui[j] = s.Pop_Stack();

j++;

}

houzhui[j] = '=';// 后綴表達(dá)式后面加 =

j++;

houzhui[j] = '\0';

j++;

return houzhui;

}

float Count(char str[])

{

Stack_Float s = new Stack_Float();// 定義存放數(shù)字的棧

char c = str[0];

int i = 0;

float result = 0, temp, left, right;

while (c != '=') // 未掃描到 = 時(shí)

{

if (c = '0' c = '9')// 掃描到數(shù)字

{

temp = 0;

while (c != '#')// 未讀到分隔符時(shí)

{

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

i++;

c = str[i];

}

s.Push_Stack(temp);// 進(jìn)棧

}

switch (c)// 掃描到運(yùn)算符時(shí)

{

case '+':

{

result = s.Pop_Stack() + s.Pop_Stack();// 2個(gè)數(shù)字出棧相加

s.Push_Stack(result);// 最后得數(shù)進(jìn)棧

break;

}

case '-':

{

right = s.Pop_Stack();// 右操作數(shù)出棧

left = s.Pop_Stack();// 左操作數(shù)出棧

result = left - right;

s.Push_Stack(result);

break;

}

case '*':

{

result = s.Pop_Stack() * s.Pop_Stack();// 2個(gè)數(shù)字出棧相乘

s.Push_Stack(result);

break;

}

case '/':

{

right = s.Pop_Stack();// 右操作數(shù)出棧

left = s.Pop_Stack();// 左操作數(shù)出棧

result = left / right;

s.Push_Stack(result);

break;

}

}

i++;

c = str[i];

}

return result;

}

int ComPare(char a, char b) // 判斷運(yùn)算符的優(yōu)先級(jí)函數(shù)

{

int s[][] = {// 棧頂元素高于算術(shù)表達(dá)式中的元素時(shí), 返回 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[] = { '+', '-', '*', '/', '(', ')', '[', ']', '{', '}' };// 算術(shù)表達(dá)式中的元素

int k = 0, m, n = 0;

for (m = 0; m 10; m++) // 查找2個(gè)進(jìn)行比較的運(yùn)算符在表中的位置,并返回比較結(jié)果

{

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

{

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

{

k = 1;

break; // 找到比較結(jié)果后,跳出循環(huán)

}

}

if (k == 1)

break;

}

return s[m][n];// 返回比較結(jié)果

}

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圖形界面簡(jiǎn)單計(jì)算器

示例代碼如下,請(qǐng)參考:

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();???//記錄運(yùn)算數(shù)字,以及保留結(jié)果

StringBuffer?s2?=?new?StringBuffer();???//記錄運(yùn)算數(shù)字,保留上一個(gè)輸入的數(shù)字或運(yùn)算結(jié)果

static?String?flag?=?new?String();??????//標(biāo)記運(yùn)算符號(hào):+,-,*,/

boolean?start?=?true;???//標(biāo)記運(yùn)算開(kāi)始或結(jié)束,保證一次運(yùn)算之后,第二次進(jìn)行運(yùn)算時(shí)能同時(shí)清空顯示界面,即s1為空

public?void?init(){

//主界面

JFrame?f?=?new?JFrame("計(jì)算器");

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

p1.add(tf);

f.add(p1,BorderLayout.NORTH);

JPanel?p2?=?new?JPanel();

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

//動(dòng)作監(jiān)聽(tīng)器

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

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

//輸入數(shù)字0~9

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

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

if(!start){

//s1清零,保證可以重新輸入數(shù)字

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

}

//輸入小數(shù)點(diǎn)

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

}

//輸入負(fù)號(hào)

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標(biāo)記設(shè)為true

start?=?true;

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

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

tf.setText(s1.toString());

}

//清除C

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

//清空當(dāng)前輸入,即s1清零

start?=?true;

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

tf.setText(s1.toString());

}

//加號(hào)

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

start?=?true;

flag="+";

//s2接收s1的值

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

//s1清零,重新接收下一個(gè)數(shù)據(jù)

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

tf.setText(s1.toString());

}

//減號(hào)

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

start?=?true;

flag="-";

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

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

tf.setText(s1.toString());

}

//乘號(hào)

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

start?=?true;

flag="*";

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

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

tf.setText(s1.toString());

}

//除號(hào)

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

start?=?true;

flag="/";

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

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

tf.setText(s1.toString());

}

//開(kāi)根號(hào)

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

}

//求結(jié)果

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

//start標(biāo)記為false

start?=false;

//分別獲取s1和s2的值

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

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

//清空s1和s2,便于開(kāi)始下一次運(yùn)算

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

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

//四則運(yùn)算

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

}

}???????

}

};

//添加按鈕以及監(jiān)聽(tīng)器

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

//調(diào)用方法,實(shí)現(xiàn)計(jì)算器

new?Cal().init();

}

}

用JAVA編寫(xiě)的科學(xué)計(jì)算器源代碼

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class Counter extends WindowAdapter

{

static JFrame f=new JFrame("計(jì)算器");

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("除數(shù)不能為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程序--編寫(xiě)一個(gè)具有圖形界面的計(jì)算器程序

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class Test implements ActionListener

{

Frame f = new Frame("一步運(yùn)算計(jì)算器");//創(chuàng)建窗架

TextField[] tf = new TextField[3];

Label La1 = new Label("+");

Button btn = new Button("=");

int[] a = new int[3]; //儲(chǔ)存數(shù)據(jù)

public Test()

{

f.setLayout(new FlowLayout());

f.setSize(400,100);

f.addWindowListener

(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

); //窗口關(guān)閉事件

btn.addActionListener //輸出加法運(yùn)算結(jié)果

(

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); //創(chuàng)建文本域,注冊(cè)事件監(jiān)聽(tīng)器

}

tf[0].requestFocusInWindow(); //光標(biāo)定位

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才能完成正確的功能.再按等號(hào)按鈕輸出結(jié)果.

JAVA計(jì)算器代碼

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("除數(shù)不能為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圖形計(jì)算器源代碼,java圖形計(jì)算器源代碼怎么用
網(wǎng)頁(yè)網(wǎng)址:http://fisionsoft.com.cn/article/hsjgid.html