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

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

java類計(jì)算器代碼 java編寫計(jì)算器類

如何用JAVA語言編寫計(jì)算器小程序?

具體代碼如下:

在克山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都營銷網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站制作,克山網(wǎng)站建設(shè)費(fèi)用合理。

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Calculator ?extends JFrame implements ActionListener ?{

private JFrame jf;

private JButton[] allButtons;

private JButton clearButton;

private JTextField jtf;

public Calculator() {

//對圖形組件實(shí)例化

jf=new JFrame("任靜的計(jì)算器1.0:JAVA版");

jf.addWindowListener(new WindowAdapter(){

public void windowClosing(){

System.exit(0);

}

});

allButtons=new JButton[16];

clearButton=new JButton("清除");

jtf=new JTextField(25);

jtf.setEditable(false);

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

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

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

}

}

public void init(){

//完成布局

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

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

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

jf.add(northPanel,BorderLayout.NORTH);

jf.add(centerPanel,BorderLayout.CENTER);

jf.add(southPanel,BorderLayout.SOUTH);

addEventHandler();

}

//添加事件監(jiān)聽

public void addEventHandler(){

jtf.addActionListener(this);

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

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

Calculator.this.jtf.setText("");

}

});

}

//事件處理

public void actionPerformed(ActionEvent e) {

//在這里完成事件處理 ?使計(jì)算器可以運(yùn)行

String action=e.getActionCommand();

if(action=="+"||action=="-"||action=="*"||action=="/"){

}

}

public void setFontAndColor(){

Font f=new Font("宋體",Font.BOLD,24);

jtf.setFont(f);

jtf.setBackground(new Color(0x8f,0xa0,0xfb));

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

allButtons[i].setFont(f);

allButtons[i].setForeground(Color.RED);

}

}

public void showMe(){

init();

setFontAndColor();

jf.pack();

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

new Calculator().showMe();

}

}

java 計(jì)算器代碼

import javax.swing.*;

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

}

}

java編程,用代碼實(shí)現(xiàn)計(jì)算器類(Calculator)

public class Calculator {

private int number1;

private int number2;

public Calculator(int number1, int number2) {

this.number1 = number1;

this.number2 = number2;

}

public double add() {

return number1 + number2;

}

public double subtract() {

return number1 - number2;

}

public double multiple() {

return number1 * number2;

}

public double divide() {

if(number2 == 0) {

return 0;

}

return number1 / number2;

}

}

public class TestCalculator {

public static void main(String[] args) {

Calculator calculator = new Calculator(5, 2);

System.out.println(calculator.add());

System.out.println(calculator.subtract());

System.out.println(calculator.multiple());

System.out.println(calculator.divide());

}

}


文章名稱:java類計(jì)算器代碼 java編寫計(jì)算器類
文章地址:http://weahome.cn/article/ddjscgi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部