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

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

java中計(jì)算器代碼詳解 java簡單計(jì)算器代碼

java 計(jì)算器代碼

import javax.swing.*;

站在用戶的角度思考問題,與客戶深入溝通,找到慈利網(wǎng)站設(shè)計(jì)與慈利網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋慈利地區(qū)。

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的計(jì)算器。Cheshi 繼承于 JFrame,是計(jì)算器的界面

c*/

public class Cheshi extends JFrame {

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public Cheshi(String title) throws HeadlessException {

super(title); // 調(diào)用父類構(gòu)造方法

setupFrame(); // 調(diào)整窗體屬性

setupControls(); // 創(chuàng)建控件

}

private void setupControls() {

setupDisplayPanel(); // 創(chuàng)建文本面板

setupButtonsPanel(); // 創(chuàng)建按鈕面板

}

// 創(chuàng)建按鈕面板并添加按鈕

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

panel.setLayout(new GridLayout(4, 5, 3, 3));

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示這個(gè)位置沒有按鈕

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上創(chuàng)建按鈕

*

* @param panel 要?jiǎng)?chuàng)建按鈕的面板

* @param labels 按鈕文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 為空,則表示創(chuàng)建一個(gè)空面板。否則創(chuàng)建一個(gè)按鈕。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 為按鈕添加偵聽器

panel.add(b);

}

}

}

// 設(shè)置顯示面板,用一個(gè)文本框來作為計(jì)算器的顯示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 調(diào)整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對齊

textbox.setEditable(false); // 文本框只讀

textbox.setBackground(Color.white); // 文本框背景色為白色

}

// 調(diào)整窗體

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時(shí)程序結(jié)束

this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置

this.setSize(300, 200); // 設(shè)置窗體大小

this.setResizable(false); // 窗體大小固定

}

// 程序入口

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

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Cheshi frame = new Cheshi("我的計(jì)算器");

frame.setVisible(true); // 在桌面上顯示窗體

}

}

/**

* 計(jì)算器核心邏輯。這個(gè)邏輯只能處理 1~2 個(gè)數(shù)的運(yùn)算。

*/

class CalculatorCore {

private String displayText = "0"; // 要顯示的文本

boolean reset = true;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 為每種命令添加處理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 為每種 operator 添加處理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根據(jù)命令處理。這里的命令實(shí)際上就是按鈕文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 當(dāng)按下 CE 時(shí)

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 當(dāng)按下 C 時(shí),將計(jì)算器置為初始狀態(tài)。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 當(dāng)按下 = 時(shí)

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 計(jì)算結(jié)果

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

}

}

// 當(dāng)按下 +-*/ 時(shí)(這里也可以擴(kuò)展成其他中間操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 當(dāng)按下 . 時(shí)

private void dotClicked() {

if (displayText.indexOf(".") == -1) {

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 當(dāng)按下 0-9 時(shí)

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

displayText += command;

}

reset = false;

}

// 運(yùn)算符處理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按鈕處理接口

interface Processor {

void calculate(String command);

}

}

關(guān)于java計(jì)算器的一段代碼,幫忙解釋一下,越詳細(xì)越好,謝謝了

你貼的代碼不全,只能給你代碼意思

String

str=e.getActionCommand().trim();//按鈕監(jiān)聽獲得的值,trim是去空格

if("0123456789.".indexOf(str)!=-1)//判斷0到9內(nèi)是否包含str這個(gè)數(shù)

{

if(jtf.getText().equals("0"))jtf.setText(null);//如果jtf(輸入框)獲取到的值=0,則設(shè)置jtf為空

jtf.setText(jtf.getText()+str);//否則設(shè)置jtf的值=jtf的值+str的值

return;

}

JAVA計(jì)算器相關(guān)代碼求大神{詳解}

我給你找找

package com.bj.calcultor;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calcultor extends Frame implements ActionListener {

public static void main(String[] args) {//定義主方

new Calcultor();//創(chuàng)建匿名對象,并調(diào)用test()方法;

}

//定義按鈕名稱

String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};

JButton [] button=new JButton[arr.length];

JButton reset = new JButton("CE");

JTextField display = new JTextField(20);

//創(chuàng)建窗口,定義組件

//執(zhí)行窗口事件:關(guān)閉窗口

private class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

}

public Calcultor(){

super("計(jì)算器");//定義標(biāo)題

//定義面板容器,并布局

JPanel jpanel=new JPanel(new GridLayout(4,4));

//添加按鈕,并給按鈕添加名稱

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

button[i]= new JButton(arr[i]);

jpanel.add(button[i]);

}

JPanel panel2 = new JPanel();

panel2.add("Northr", display);

display.setEnabled(false);

panel2.add("East", reset);

this.add("North", panel2);

this.add("Center", jpanel);

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

addWindowListener(new WindowCloser());

setVisible(true);

setSize(400,400);

pack();

button[i].addActionListener(this);

reset.addActionListener(this);

display.addActionListener(this);

}

}

@Override

public void actionPerformed(ActionEvent e) {//定義事件

// TODO Auto-generated method stub

Object target=e.getSource();

String lable=e.getActionCommand();

if(target==reset){

handleReset();

}else if("0123456789.".indexOf(lable)0){

handleNumber(lable);

}else{

hadleOperator(lable);

}

}

boolean isFirstDigit=true;

private void hadleOperator(String key) {

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

number += Double.valueOf(display.getText());

}else if (operator.equals("-")){

number -= Double.valueOf(display.getText());

}else if (operator.equals("*")){

number *= Double.valueOf(display.getText());

}else if (operator.equals("/")){

number /= Double.valueOf(display.getText());

}else if(operator.equals("=")){

number =Double.valueOf(display.getText());

}

display.setText(String.valueOf(number));

operator=key;

isFirstDigit=true;

}

private void handleNumber(String key) {

if(true){

display.setText(key);//把鍵值設(shè)置為文本框內(nèi)容

}else if(key.equals(".") display.getText().indexOf(".")0){

display.setText(display.getText()+".");//把文本框內(nèi)容設(shè)置:display.getText()+"."

}else if(!key.equals(".")){

display.setText(display.getText() + key);//把文本框內(nèi)容設(shè)置:display.getText()+key

isFirstDigit=false;

}

}

private void handleReset() {

display.setText("0");

isFirstDigit=true;

operator="=";

}

String operator="=";

Double number=0.0;

}


標(biāo)題名稱:java中計(jì)算器代碼詳解 java簡單計(jì)算器代碼
分享URL:http://weahome.cn/article/hgppph.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部