真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java計算器按鈕代碼 java實現(xiàn)計算器功能

JAVA 編寫計算器 要代碼最簡單的

學(xué)java的時候自己編的,很簡單,能夠連續(xù)輸入計算式后進(jìn)行計算

創(chuàng)新互聯(lián)建站長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為市中企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、做網(wǎng)站市中網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**簡易計算器,能夠進(jìn)行簡單的計算

*

* @see 2008.12.9

*/

public class CalculatorA

implements ActionListener{

private JFrame frame;

private JTextField field;

private JButton[] allButtons;

private JButton clearButton;

// private JButton backButton;

String result="";//保存結(jié)果

StringBuilder sb = new StringBuilder();//保存要進(jìn)行的計算式

int x = 0; //用來判斷上一次的事件類型

String str = "123+456-789*0.=/";

ArrayListString arrayList = new ArrayListString();//保存計算式,通過方法進(jìn)行運算

public CalculatorA(){

frame = new JFrame("我的計算器v1.1");

frame.setLocation(300,300);

field = new JTextField(25);

allButtons = new JButton[16];

for(int i=0;iallButtons.length;i++){

allButtons[i]= new JButton(str.substring(i,i+1));

}

clearButton = new JButton("CLEAR");

// backButton = new JButton("——");

init();

setFondAndColor();

addEventHander();

}

public void init(){

frame.setLayout(new BorderLayout());

JPanel northPanel = new JPanel();

JPanel centerPanel = new JPanel();

JPanel southPanel = new JPanel();

northPanel.setLayout(new FlowLayout());

centerPanel.setLayout(new GridLayout(4,4));

southPanel.setLayout(new FlowLayout());

northPanel.add(field);

for(int i=0;iallButtons.length;i++){

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

//southPanel.add(backButton);

frame.add(northPanel,BorderLayout.NORTH);

frame.add(centerPanel,BorderLayout.CENTER);

frame.add(southPanel,BorderLayout.SOUTH);

}

//設(shè)置輸入字體

public void setFondAndColor(){

field.setFont(new Font("宋體",Font.BOLD,24));

field.setBackground(new Color(100,200,200));

field.setForeground(Color.RED);

//設(shè)置字體從右起始

field.setHorizontalAlignment(JTextField.RIGHT);

}

public void showMi(){

frame.pack();

frame.setResizable(false);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void addEventHander(){

for(int i=0;iallButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(this);

// backButton.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();//取得當(dāng)前事件返回的值

if("0123456789.".indexOf(str)!=-1){

if(x == 0){ //當(dāng)x為0時表示還沒有進(jìn)行輸入

result=str;

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 1){

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 2){

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

result = result+str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 3){

result = str;

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

arrayList.clear();

field.setText(str);

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 4){

result ="";

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

arrayList.clear();

result = str;

sb.append(str);

field.setText(str);

x = 1;

}

else{

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

}

else if("+*-/".indexOf(str)!=-1){

if(x == 0){

field.setText("");

x = 2;

}

else if(x == 1){

result = result + str;

arrayList.add(sb.toString());

arrayList.add(str);

sb.append(str);

field.setText(result);

x = 2;

}

else if(x == 2){

x = 2;

}

else if(x == 3){

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

else if(x == 4){

result ="";

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

arrayList.clear();

x = 2;

}

else{

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

}

else if("=".equals(str)){

if(x == 0){

field.setText("0");

arrayList.clear();

result = "0";

x = 3;

}

else if(x == 1){

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("數(shù)據(jù)格式異常");

x = 0;

}

}

else if(x == 2){

field.setText("數(shù)據(jù)格式錯誤.....");

arrayList.clear();

x = 0;

}

else if(x == 3){

field.setText(result);

x = 3;

}

else if(x == 4){

result ="";

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

arrayList.clear();

x = 3;

}

else {

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("數(shù)據(jù)格式異常");

x = 0;

}

}

}

else if("CLEAR".equals(str)){

arrayList.clear();

field.setText("0");

arrayList.add("0");

x = 4;

}

else{

if(result.length()1){

result = result.substring(0,result.length()-1);

if(sb.length()0){

sb.delete(sb.length()-1,sb.length());

}

else {

sb.delete(0,1);

}

field.setText(result);

x = 5;

}

else{

result = "";

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

arrayList.clear();

field.setText("0");

x = 0;

}

}

}

public static ArrayListString getResult(ArrayListString list){

String res = null;

String[] s = {"/","*","-","+"};

int i=0;

if(list.size()1){

for(;is.length;){

if(s[i].equals("/")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));

//本地的數(shù)據(jù)格式

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("*")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("-")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getNumberInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else {

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

}

}

return list;

}

//對數(shù)字字符串進(jìn)行排除不必要符號

public static String getChange(String res){

String s_temp = "";

char[] c = new char[res.length()];

for(int k=0;kc.length;k++){

c[k] = res.charAt(k);

}

for(int k=0;kc.length;k++){

if((c[k]= '0' c[k]= '9')|| c[k] == '.'){

s_temp += c[k];

}

}

return s_temp;

}

public static void main(String[] args){

new CalculatorA().showMi();

}

}

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("除數(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實現(xiàn)計算器的代碼(可實用的,沒語法錯誤的)

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; //導(dǎo)包 public class Jisuanqi extends JFrame implements ActionListener { //繼承JFrame 實現(xiàn)事件監(jiān)聽 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() { //構(gòu)造屬性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("輸出"); tb.setTitleColor(Color.BLUE); //標(biāo)題邊框底端線 設(shè)置顏色 btnAbout = new JButton(" 關(guān)于 "); btnCancel = new JButton("Cancel"); //兩個按鈕 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //給Cancel添加事件監(jiān)聽 當(dāng)鼠標(biāo)點擊時 程序結(jié)束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黃蓋??!", "消息", JOptionPane.INFORMATION_MESSAGE); } //給“關(guān)于”添加事件監(jiān)聽 當(dāng)鼠標(biāo)點擊時 彈出對話框 顯示黃蓋 }); 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); //設(shè)置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 設(shè)置文本的水平對齊方式。 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); //事件監(jiān)聽 + 顏色 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)); //面板上設(shè)置對齊方式 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) { // 如果數(shù)字輸入結(jié)束,則將文本框置零,重新輸入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的內(nèi)容為零,則覆蓋文本框的內(nèi)容 reasult.setText(s); } else { // 如果文本框的內(nèi)容不為零,則在內(nèi)容后面添加數(shù)字 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; } //設(shè)計計算的過程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-簡易計算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默認(rèn)關(guān)閉可以關(guān)閉程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 這個計算機,絕對讓你滿意

用java.EE編寫計算器程序代碼

java EE是java企業(yè)級開發(fā)平臺的意思,實在是看不出跟計算器這種小程序有什么關(guān)聯(lián)。不知道樓主要找的是不是這個。

package ex1;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.DecimalFormat;

import javax.swing.BorderFactory;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JRadioButtonMenuItem;

import javax.swing.JTextField;

public class Calcutor extends JFrame {

private JTextField tf;

private JPanel panel1, panel2, panel3, panel4;

private JMenuBar myBar;

private JMenu menu1, menu2, menu3;

private JMenuItem editItem1, editItem2, help1, help2, help3;

private JRadioButtonMenuItem seeItem1, seeItem2;//單選框

private JCheckBoxMenuItem seeItem3;//復(fù)選框

private ButtonGroup bgb;

private String back;

private boolean IfResult = true, flag = false;

private String oper = "=";

private double result = 0;

private Num numActionListener;

private DecimalFormat df;

public Calcutor(){

super("科學(xué)計算器");//設(shè)置標(biāo)題欄

df = new DecimalFormat("#.####");//保留四位小數(shù)

this.setLayout(new BorderLayout(10, 5));

panel1 = new JPanel(new GridLayout(1, 3, 10, 10));

panel2 = new JPanel(new GridLayout(5, 6, 5, 5));//5行6列

panel3 = new JPanel(new GridLayout(5, 1, 5, 5));

panel4 = new JPanel(new BorderLayout(5, 5));

/**

* 菜單欄

*/

myBar = new JMenuBar();

menu1 = new JMenu("編輯(E)");

menu2 = new JMenu("查看(V)");

menu3 = new JMenu("幫助(H)");

menu1.setFont(new Font("宋體", Font.PLAIN, 12));

menu2.setFont(new Font("宋體", Font.PLAIN, 12));

menu3.setFont(new Font("宋體", Font.PLAIN, 12));

/**

* 編輯欄

*/

editItem1 = new JMenuItem("復(fù)制(C) Ctrl+C");

editItem2 = new JMenuItem("粘貼(P) Ctrl+V");

editItem1.setFont(new Font("宋體",Font.PLAIN,12));

editItem2.setFont(new Font("宋體",Font.PLAIN,12));

/**

* 查看欄

*/

seeItem1 = new JRadioButtonMenuItem("科學(xué)型(T)");

seeItem2 = new JRadioButtonMenuItem("標(biāo)準(zhǔn)型(S)");

seeItem3 = new JCheckBoxMenuItem("數(shù)字分組(I)");

seeItem1.setFont(new Font("宋體",Font.PLAIN,12));

seeItem2.setFont(new Font("宋體",Font.PLAIN,12));

seeItem3.setFont(new Font("宋體",Font.PLAIN,12));

/**

* 幫助欄

*/

help1 = new JMenuItem("幫助主題(H)");

help2 = new JMenuItem("關(guān)于計算器(A)");

help1.setFont(new Font("宋體",Font.PLAIN,12));

help2.setFont(new Font("宋體",Font.PLAIN,12));

bgb = new ButtonGroup();//選項組

menu1.add(editItem1);

menu1.add(editItem2);

menu2.add(seeItem1);

menu2.add(seeItem2);

menu2.addSeparator();//添加一條分割線

menu2.add(seeItem3);

menu3.add(help1);

menu3.addSeparator();//添加一條分割線

menu3.add(help2);

myBar.add(menu1);

myBar.add(menu2);

myBar.add(menu3);

this.setJMenuBar(myBar);

numActionListener = new Num();//實現(xiàn)數(shù)字監(jiān)聽

/**

* 文本域,即為計算器的屏幕顯示區(qū)域

*/

tf = new JTextField();

tf.setEditable(false);//文本區(qū)域不可編輯

tf.setBackground(Color.white);//文本區(qū)域的背景色

tf.setHorizontalAlignment(JTextField.RIGHT);//文字右對齊

tf.setText("0");

tf.setBorder(BorderFactory.createLoweredBevelBorder());

init();//對計算器進(jìn)行初始化

}

/**

* 初始化操作

* 添加按鈕

*/

private void init(){

addButton(panel1, "Backspace", new Clear(), Color.red);

addButton(panel1, "CE", new Clear(), Color.red);

addButton(panel1, "C", new Clear(), Color.red);

addButton(panel2, "1/x", new Signs(), Color.magenta);

addButton(panel2, "log", new Signs(), Color.magenta);

addButton(panel2, "7", numActionListener, Color.blue);

addButton(panel2, "8", numActionListener, Color.blue);

addButton(panel2, "9", numActionListener, Color.blue);

addButton(panel2, "÷", new Signs(), Color.red);

addButton(panel2, "n!", new Signs(), Color.magenta);

addButton(panel2, "sqrt", new Signs(), Color.magenta);

addButton(panel2, "4", numActionListener, Color.blue);

addButton(panel2, "5", numActionListener, Color.blue);

addButton(panel2, "6", numActionListener, Color.blue);

addButton(panel2, "×", new Signs(), Color.red);

addButton(panel2, "sin", new Signs(), Color.magenta);

addButton(panel2, "x^2", new Signs(), Color.magenta);

addButton(panel2, "1", numActionListener, Color.blue);

addButton(panel2, "2", numActionListener, Color.blue);

addButton(panel2, "3", numActionListener, Color.blue);

addButton(panel2, "-", new Signs(), Color.red);

addButton(panel2, "cos", new Signs(), Color.magenta);

addButton(panel2, "x^3", new Signs(), Color.magenta);

addButton(panel2, "0", numActionListener, Color.blue);

addButton(panel2, "-/+", new Clear(), Color.blue);

addButton(panel2, ".", new Dot(), Color.blue);

addButton(panel2, "+", new Signs(), Color.red);

addButton(panel2, "tan", new Signs(), Color.magenta);

addButton(panel2, "%", new Signs(), Color.magenta);

addButton(panel2, "π", numActionListener, Color.orange);

addButton(panel2, "e", numActionListener, Color.orange);

addButton(panel2, "′″", new Signs(), Color.orange);

addButton(panel2, "=", new Signs(), Color.red);

JButton btns = new JButton("計算器");

btns.setBorder(BorderFactory.createLoweredBevelBorder());

btns.setEnabled(false);//按鈕不可操作

btns.setPreferredSize(new Dimension(20, 20));

panel3.add(btns);//加入按鈕

addButton(panel3, "MC", null, Color.red);

addButton(panel3, "MR", null, Color.red);

addButton(panel3, "MS", null, Color.red);

addButton(panel3, "M+", null, Color.red);

panel4.add(panel1, BorderLayout.NORTH);

panel4.add(panel2, BorderLayout.CENTER);

this.add(tf, BorderLayout.NORTH);

this.add(panel3, BorderLayout.WEST);

this.add(panel4);

pack();

this.setResizable(false);//窗口不可改變大小

this.setLocation(300, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

/**

* 統(tǒng)一設(shè)置按鈕的的使用方式

* @param panel

* @param name

* @param action

* @param color

*/

private void addButton(JPanel panel, String name, ActionListener action, Color color){

JButton bt = new JButton(name);

panel.add(bt);//在面板上增加按鈕

bt.setForeground(color);//設(shè)置前景(字體)顏色

bt.addActionListener(action);//增加監(jiān)聽事件

}

/**

* 計算器的基礎(chǔ)操作(+ - × ÷)

* @param x

*/

private void getResult (double x){

if(oper == "+"){result += x;}

else if(oper == "-"){result -= x;}

else if(oper == "×"){result *= x;}

else if(oper == "÷"){result /= x;}

else if(oper == "="){result = x;}

tf.setText(df.format(result));

}

/**

* 運算符號的事件監(jiān)聽

*/

class Signs implements ActionListener{

public void actionPerformed(ActionEvent e) {

/*

* 用ActionEvent對象的getActionCommand()方法

* 取得與引發(fā)事件對象相關(guān)的字符串

*/

String str = e.getActionCommand();

/* sqrt求平方根 */

if(str.equals("sqrt")){

double i = Double.parseDouble(tf.getText());

if(i=0){

/*

* String.valueOf() 轉(zhuǎn)換為字符串

* df.format() 按要求保留四位小數(shù)

* Math.sqrt() 求算數(shù)平方根

*/

tf.setText(String.valueOf(df.format(Math.sqrt(i))));

}

else{

tf.setText("負(fù)數(shù)不能開平方根");

}

}

/* log求常用對數(shù) */

else if(str.equals("log")){

double i = Double.parseDouble(tf.getText());

if(i0){

tf.setText(String.valueOf(df.format(Math.log(i))));

}else{

tf.setText("負(fù)數(shù)不能求對數(shù)");

}

}

/* %求百分比 */

else if(str.equals("%")){

tf.setText(df.format(Double.parseDouble(tf.getText()) / 100));

}

/* 1/x求倒數(shù) */

else if(str.equals("1/x")){

if(Double.parseDouble(tf.getText()) == 0){

tf.setText("除數(shù)不能為零");

}else{

tf.setText(df.format(1 / Double.parseDouble(tf.getText())));

}

}

/* sin求正弦函數(shù) */

else if(str.equals("sin")){

double i = Double.parseDouble(tf.getText());

tf.setText(String.valueOf(df.format(Math.sin(i))));

}

/* cos求余弦函數(shù) */

else if(str.equals("cos")){

double i = Double.parseDouble(tf.getText());

tf.setText(String.valueOf(df.format(Math.cos(i))));

}

/* tan求正切函數(shù) */

else if(str.equals("tan")){

double i = Double.parseDouble(tf.getText());

tf.setText(String.valueOf(df.format(Math.tan(i))));

}

/* n!求階乘 */

else if(str.equals("n!")){

double i = Double.parseDouble(tf.getText());

if((i%2==0)||(i%2==1))//判斷為整數(shù)放進(jìn)行階乘操作

{

int j = (int)i;//強制類型轉(zhuǎn)換

int result=1;

for(int k=1;k=j;k++)

result *= k;

tf.setText(String.valueOf(result));

}

else

{

tf.setText("無法進(jìn)行階乘");

}

}

/* x^2求平方 */

else if(str.equals("x^2")){

double i = Double.parseDouble(tf.getText());

tf.setText(String.valueOf(df.format(i*i)));

}

/* x^3求立方 */

else if(str.equals("x^3")){

double i = Double.parseDouble(tf.getText());

tf.setText(String.valueOf(df.format(i*i*i)));

}

/* ′″角度轉(zhuǎn)換 */

/**

* 將角度值轉(zhuǎn)換成弧度值,方便三角函數(shù)的計算

*/

else if(str.equals("′″")){

double i = Double.parseDouble(tf.getText());

tf.setText(String.valueOf(i/180*Math.PI));

}

else{

if(flag){

IfResult = false;

}

if(IfResult){

oper = str;

}else{

getResult(Double.parseDouble(tf.getText()));

oper = str;

IfResult = true;

}

}

}

}

/**

* 清除按鈕的事件監(jiān)聽

*/

class Clear implements ActionListener{

public void actionPerformed(ActionEvent e) {

/*

* 用ActionEvent對象的getActionCommand()方法

* 取得與引發(fā)事件對象相關(guān)的字符串

*/

String str = e.getActionCommand();

if(str == "C"){

tf.setText("0");

IfResult = true;

result = 0;

}else if(str == "-/+"){

double i = 0 - Double.parseDouble(tf.getText().trim());

tf.setText(df.format(i));

}else if(str == "Backspace"){

if(Double.parseDouble(tf.getText()) 0){

if(tf.getText().length() 1){

tf.setText(tf.getText().substring(0, tf.getText().length() - 1));

//使用退格刪除最后一位字符

}else{

tf.setText("0");

IfResult = true;

}

}else{

if(tf.getText().length() 2){

tf.setText(tf.getText().substring(0, tf.getText().length() - 1));

}else{

tf.setText("0");

IfResult = true;

}

}

}else if(str == "CE"){

tf.setText("0");

IfResult = true;

}

}

}

/**

* 數(shù)字輸入的事件監(jiān)聽

*/

class Num implements ActionListener{

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();

if(IfResult){

tf.setText("");

IfResult = false;

}

if(str=="π")

{

tf.setText(String.valueOf(Math.PI));

}

else if(str=="e")

{

tf.setText(String.valueOf(Math.E));

}

else{

tf.setText(tf.getText().trim() + str);

if(tf.getText().equals("0")){

tf.setText("0");

IfResult = true;

flag = true;

}

}

}

}

/**

* 小數(shù)點的事件監(jiān)聽

*/

class Dot implements ActionListener{

public void actionPerformed(ActionEvent e) {

IfResult = false;

if(tf.getText().trim().indexOf(".") == -1){

tf.setText(tf.getText() + ".");

}

}

}

/**

* main方法

*/

public static void main(String[] args) {

new Calcutor().setVisible(true);

}

}

java計算器16個按鈕(要用for循環(huán)打出來的)源代碼

自己寫的,給你參考參考import java.awt.BorderLayout;

import java.awt.FlowLayout;

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;public class Counter implements ActionListener{ /**

* @param args

*/

private JFrame frame;

private JTextField field;

private JButton clear_but;

private JButton[] buttons;

String str="123+456-789*0.=/";

public Counter() {

frame=new JFrame("計算器t\t\t\t\t\t\t");

field=new JTextField(15);

clear_but=new JButton("C");

buttons=new JButton[25];

for(int i=0;istr.length();i++)

{

buttons[i]=new JButton(str.substring(i, i+1));

}

field.setText("");

}

private void init(){

addEventHandler();

frame.setLayout(new BorderLayout());

JPanel n_panel=new JPanel();

JPanel c_panel=new JPanel();

n_panel.setLayout(new FlowLayout());

frame.add(n_panel,BorderLayout.NORTH);

n_panel.add(field);

n_panel.add(clear_but);

c_panel.setLayout(new GridLayout(4,4));

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

{

c_panel.add(buttons[i]);

}

frame.add(c_panel, BorderLayout.CENTER);

}

public void showMe(){

init();

frame.setSize(300, 240);

//frame.pack();// frame.setLocation(400,360);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void addEventHandler(){

//clear_but.addActionListener(new MyActionListener());

clear_but.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

field.setText("");

}

});

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

{

final int in=i;

buttons[i].addActionListener(this);

// @Override

// public void actionPerformed(ActionEvent e) {

// System.out.print(e.getActionCommand());

// //System.out.print(str.substring(in, in+1));

// }

//

}

}

String num="";

String flag="";

//@Override

public void actionPerformed(ActionEvent e) {

String str=e.getActionCommand();

if(str.equals("=")){

Double a=Double.valueOf(num);

Double b=Double.valueOf(field.getText());

double sum=0;

if(flag.equals("+")){

sum=a+b;

field.setText(sum+"");

}

if(flag.equals("-")){

sum=a-b;

field.setText(""+sum);

}

if(flag.equals("*")){

sum=a*b;

field.setText(""+sum);

}

if(flag.equals("/")){

sum=a/b;

field.setText(""+sum);

}

System.out.println(a+flag+b+"="+sum);

}

else if(".0123456789".indexOf(str)!=-1){

String newtext=field.getText();

newtext+=str;

field.setText(newtext);

//System.out.print(str);

}

else if(str.matches("[+*-/]{1}")){

//else if("+-*/".indexOf(str)!=-1){

num=field.getText();

field.setText("");

flag=str;

// System.out.print(str);

}

}

public static void main(String[] args) {

new Counter().showMe();

}}

怎么用JAVA編程編寫一個計算器?

打開IED:打開自己java編程的軟件,采用的是eclipse軟件。

建立java工程。

編寫類。

編寫類的詳細(xì)步驟

1.類的基本結(jié)構(gòu):

由于這里用到了界面,所以要進(jìn)行窗口界面的編程,按鈕事件的處理,和計算處理界面;

package MyCaculator;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyCaculator extends JFrame {

private int add=1,sub=2,mul=3,div=4;

private int op=0;

boolean ifOp;

private String output="0";

private Button[] jba=new Button[]{new Button("7"),new Button("8"),

new Button("9"),new Button("+"),

new Button("4"),new Button("5"),new Button("6"),new Button("-"),

new Button("1"),new Button("2"),new Button("3"),new Button("*"),

new Button("0"),new Button("."),new Button("="),new Button("/")};

private JPanel jpt=new JPanel();

private JPanel jpb=new JPanel();

private JTextField jtf=new JTextField("");

private MyCaculator(){

}

private void operate(String x){

}

public String add(String x){

return output;

}

public String subtract(String x){

return output;

}

public String multiply(String x){

return output;

}

public String divide(String x){

return output;

}

public String Equals(String x){

return output;

}

public void opClean(){

}

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

public static void main(String[] args) {

}

}

2.建立構(gòu)造方法:

所謂構(gòu)造方法就是,對自己的主類進(jìn)行初始化,代碼如下:

private MyCaculator(){

jpt.setLayout(new BorderLayout());

jpt.add(jtf);

this.add(jpt,BorderLayout.NORTH);

jpb.setLayout(new GridLayout(4,4));

for(int i=0;ijba.length;i++){

jpb.add(jba[i]);

if(i==3||i==7||i==11||i==15||i==14)

jba[i].addActionListener(new setOperate_Act());

else

jba[i].addActionListener(new setLabel_Act());

}

this.add(jpb,BorderLayout.CENTER);

this.setSize(250, 200);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

3.建立數(shù)據(jù)計算方法

這里的數(shù)據(jù)計算方法有6個,一個是主方法其他幾個是加減乘除的處理方法,代碼如下:

private void operate(String x){

double x1=Double.valueOf(x);

double y=Double.valueOf(output);

switch(op){

case 0:output=x;break;

case 1:output=String.valueOf(y+x1);break;

case 2:output =String.valueOf(y-x1);break;

case 3:output =String.valueOf(y*x1);break;

case 4:

if(x1!=0) output=String.valueOf(y/x1);

else output="不能為0";

break;

}

}

public String add(String x){

operate(x);

op=add;

return output;

}

public String subtract(String x){

operate(x);

op=sub;

return output;

}

public String multiply(String x){

operate(x);

op=mul;

return output;

}

public String divide(String x){

operate(x);

op=div;

return output;

}

public String Equals(String x){

operate(x);

op=0;

return output;

}

public void opClean(){

op=0;

output ="0";

}

4.事件處理方法

這里的時間處理方法,沒有建立一個整體的方法,二是在為了便于處理的方法,將按鈕事件分成兩個部分,并采用兩個子類來實現(xiàn),這兩個類時內(nèi)部類要寫在主類內(nèi)部的,代碼如下:

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jba[3]){

jtf.setText(add(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[7]){

jtf.setText(subtract(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[11]){

jtf.setText(multiply(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[15]){

jtf.setText(divide(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[14]){

jtf.setText(Equals(jtf.getText()));

ifOp=true;

}

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

Button tempb=(Button)e.getSource();

if(ifOp){

jtf.setText(tempb.getLabel());

ifOp=false;

}else {

jtf.setText(jtf.getText()+tempb.getLabel());

}

}

}

5.建立main方法:

要想實現(xiàn)我們的代碼,我們需在main方法中,實例化我們的對象。

public static void main(String[] args) {

new MyCaculator();

}


分享名稱:java計算器按鈕代碼 java實現(xiàn)計算器功能
URL分享:http://weahome.cn/article/hideph.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部