「用java编写简单计算器」用java编写简单计算器程序

博主:adminadmin 2023-03-19 12:31:08 337

今天给各位分享用java编写简单计算器的知识,其中也会对用java编写简单计算器程序进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何用JAVA语言编写计算器小程序?

具体代码如下:

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

//对图形组件实例化

jf=new JFrame("任静的计算器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();

}

//添加事件监听

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

//在这里完成事件处理  使计算器可以运行

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编写的计算器

package main;

import java.awt.Button;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.math.BigDecimal;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

static Panel pan = new Panel();

static JTextField textField = new JTextField("0");

static Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp, ba, bs, bm, bd,

be, bc, bt, bf, bh;

private StringBuffer temp = new StringBuffer("");

private String optValue = "0";

private String optType="";

private boolean isChoiseOptType=true;

public void init() {

b0 = new Button("0");

b0.addActionListener(this);

b1 = new Button("1");

b1.addActionListener(this);

b2 = new Button("2");

b2.addActionListener(this);

b3 = new Button("3");

b3.addActionListener(this);

b4 = new Button("4");

b4.addActionListener(this);

b5 = new Button("5");

b5.addActionListener(this);

b6 = new Button("6");

b6.addActionListener(this);

b7 = new Button("7");

b7.addActionListener(this);

b8 = new Button("8");

b8.addActionListener(this);

b9 = new Button("9");

b9.addActionListener(this);

bp = new Button(".");

bp.addActionListener(this);

ba = new Button("+");

ba.addActionListener(this);

bs = new Button("-");

bs.addActionListener(this);

bm = new Button("*");

bm.addActionListener(this);

bd = new Button("/");

bd.addActionListener(this);

be = new Button("=");

be.addActionListener(this);

bc = new Button("c");

bc.addActionListener(this);

bt = new Button("退格");

bt.addActionListener(this);

bf = new Button("1/x");

bf.addActionListener(this);

bh = new Button("+/-");

bh.addActionListener(this);

this.setTitle("计算机");

this.setLayout(null);

this.setSize(260, 300);

this.setResizable(false);

GridLayout grid = new GridLayout(4, 5);

pan.setLayout(grid);

pan.setBounds(20, 60, 150, 120);

textField.setBounds(20, 35, 150, 20);

textField.setBackground(Color.cyan);

textField.setHorizontalAlignment(textField.RIGHT);

textField.setEditable(false);

pan.add(b1);

pan.add(b2);

pan.add(b3);

pan.add(ba);

pan.add(bc);

pan.add(b4);

pan.add(b5);

pan.add(b6);

pan.add(bs);

pan.add(bt);

pan.add(b7);

pan.add(b8);

pan.add(b9);

pan.add(bm);

pan.add(bf);

pan.add(b0);

pan.add(bh);

pan.add(bp);

pan.add(bd);

pan.add(be);

this.add(textField);

this.add(pan);

}

public static void main(String[] args) {

Calculator frm = new Calculator();

frm.init();

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setVisible(true);

}

@SuppressWarnings("static-access")

@Override

public void actionPerformed(ActionEvent e) {

String value="0";

if (e.getSource().equals(b0)) {

this.temp.append(b0.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b1)) {

this.temp.append(b1.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b2)) {

this.temp.append(b2.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b3)) {

this.temp.append(b3.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b4)) {

this.temp.append(b4.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b5)) {

this.temp.append(b5.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b6)) {

this.temp.append(b6.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b7)) {

this.temp.append(b7.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b8)) {

this.temp.append(b8.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b9)) {

this.temp.append(b9.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(bp)) {

if(this.temp.length()=0)

this.temp.append("0");

this.temp.append(bp.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(ba)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=ba.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bs)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bs.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bm)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bm.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bd)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bd.getLabel();

isChoiseOptType=true;

}else if (e.getSource().equals(be)) {

if(!this.optType.equals("")){

BigDecimal opt1=new BigDecimal(this.optValue);

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

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

}

BigDecimal opt2=new BigDecimal(value);

BigDecimal result=new BigDecimal(0);

if(this.optType.equals("+")){

result=opt1.add(opt2);

}else if(this.optType.equals("-")){

result=opt1.subtract(opt2);

}else if(this.optType.equals("*")){

result=opt1.multiply(opt2);

}else if(this.optType.equals("/")){

result=opt1.divide(opt2);

}else if(this.optType.equals("%")){

result=opt1.remainder(opt2);

}

this.textField.setText(result.toString());

this.temp=new StringBuffer("");

isChoiseOptType=false;

this.optValue="0";

}

} else if (e.getSource().equals(bc)) {

this.temp=new StringBuffer();

this.textField.setText("0");

} else if (e.getSource().equals(bt)) {

value=this.textField.getText();

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

if(value.indexOf("-")=0 value.length()=1){

value="0";

this.temp=new StringBuffer("");

}else{

this.temp=new StringBuffer(value);

}

this.textField.setText(value);

}else if (e.getSource().equals(bh)) {

value=this.textField.getText();

if(value.indexOf("-")==0){

value=value.substring(1,value.length());

}else{

value="-"+value;

}

this.temp=new StringBuffer(value);

this.textField.setText(value);

} else if (e.getSource().equals(bf)) {

this.optValue=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

this.optValue=this.optValue.substring(0,this.optValue.length()-1);

}

Integer opt1=new Integer(this.optValue);

if(!opt1.toString().equals("0")){

this.textField.setText(1.0/opt1.intValue()+"");

System.out.println(1/opt1.intValue()+"");

}else{

this.textField.setText("0");

}

this.temp=new StringBuffer("");

this.optType="";

this.optValue="0";

}

}

}

编写java程序简单计算器

主要涉及的知识点: 类的写法, 以及方法的调用 .建议多做练习. 如果有看不懂的地方. 可以继续追问,一起讨论.

参考代码如下

//Number类

class Number {

private int n1;//私有的整型数据成员n1

private int n2;//私有的整型数据成员n2

// 通过构造函数给n1和n2赋值

public Number(int n1, int n2) {

this.n1 = n1;

this.n2 = n2;

}

// 加法

public int addition() {

return n1 + n2;

}

// 减法

public int subtration() {

return n1 - n2;

}

// 乘法

public int multiplication() {

return n1 * n2;

}

// 除法 (可能除不尽,所以使用double作为返回类型)

public double division() {

return n1 * 1.0 / n2; // 通过n1*1.0 把计算结果转换成double类型.

}

}

//Exam4 类

public class Exam4{

public static void main(String[] args) {

Number number=new Number(15, 6);//创建Number类的对象

//下面的是调用方法得到返回值进行输出显示

System.out.println("加法"+number.addition());

System.out.println("减法"+number.subtration());

System.out.println("乘法"+number.multiplication());

System.out.println("除法"+number.division());

}

}

用java编写一个简单计算器

package swing;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")

public class Calculator extends JFrame{

JTextField jt1 ,jt2 ,jt3;

JLabel jLabel;

JButton equButton,addButton,reduceButton,mulButton,divButton;

public Calculator() {

super("简易计算器");

JPanel contentPanel = new JPanel();

contentPanel.setLayout(new GridLayout(2, 0));

JPanel uJPanel = new JPanel();

Dimension preferredSize = new Dimension(50, 29);

GridBagConstraints gbc = new GridBagConstraints();

uJPanel.setLayout(new GridBagLayout());

gbc.gridx = GridBagConstraints.RELATIVE;

gbc.gridy = 0;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.insets = new Insets(1, 2, 1, 2);

jt1 = new JTextField(4);

jLabel = new JLabel();

jLabel.setPreferredSize(new Dimension(10, 29));

jt2 = new JTextField(4);

equButton = new JButton("=");

ActionListener myActionListener = new MyActionListener();

equButton.addActionListener(myActionListener);

jt3 = new JTextField(8);

jt1.setPreferredSize(preferredSize);

jt2.setPreferredSize(preferredSize);

jt3.setPreferredSize(preferredSize);

uJPanel.add(jt1,gbc);

uJPanel.add(jLabel,gbc);

uJPanel.add(jt2,gbc);

uJPanel.add(equButton,gbc);

gbc.weightx = 1;

uJPanel.add(jt3,gbc);

contentPanel.add(uJPanel);

JPanel dJPanel = new JPanel();

dJPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 2));

addButton = new JButton("+");

reduceButton = new JButton("-");

mulButton = new JButton("*");

divButton = new JButton("/");

addButton.addActionListener(myActionListener);

reduceButton.addActionListener(myActionListener);

mulButton.addActionListener(myActionListener);

divButton.addActionListener(myActionListener);

dJPanel.add(addButton);

dJPanel.add(reduceButton);

dJPanel.add(mulButton);

dJPanel.add(divButton);

contentPanel.add(dJPanel);

this.setContentPane(contentPanel);

this.pack();

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(3);

this.setVisible(true);

}

class MyActionListener implements ActionListener{

Double d1,d2,d3;

String operator = "";

public void actionPerformed(ActionEvent e) {

String fun = e.getActionCommand();

if (!fun.equals("=")) {

jLabel.setText(fun);

operator = fun;

}else {

d1 = jt1.getText().equals("")?null:Double.valueOf(jt1.getText());

d2 = jt2.getText().equals("")?null:Double.valueOf(jt2.getText());

d3 = calculate(d1,d2,operator);

jt3.setText(d3==null ? "":d3.toString());

jt3.setCaretPosition(0);

}

}

}

Double calculate(Double d1,Double d2,String operator){

if (d1 == null || d2 == null) {

return null;

}

Double d3 = null;

switch (operator) {

case "+":

d3 = d1 + d2;

break;

case "-":

d3 = d1 - d2;

break;

case "*":

d3 = d1 * d2;

break;

case "/":

d3 = d1 / d2;

break;

}

return d3;

}

public static void main(String[] args) {

new Calculator();

}

}

关于用java编写简单计算器和用java编写简单计算器程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。