java编程实现计算器

网上科普有关“java编程实现计算器”话题很是火热,小编也是针对java编程实现计算器寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。i...

网上科普有关“java编程实现计算器”话题很是火热,小编也是针对java编程实现计算器寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

import java.awt.Dialog;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Label;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.Panel;

import java.awt.TextField;

import java.awt.Toolkit;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.StringSelection;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Calculator extends Frame implements ActionListener {

private String s_show;

private String s_go;

Panel p1 = new Panel();

Panel p2 = new Panel();

Panel p3=new Panel();

Oper do1 = new Oper();

TextField textfields = new TextField(27);

Dialog dlog=new Dialog(this,"about us");

Label aboutlab=new Label();

String s1;

String sedit;

Button buttons1 = new Button("Backspace");

int screenWidth,screenHeight;

Clipboard clp=Toolkit.getDefaultToolkit().getSystemClipboard();

Calculator() {

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

MenuBar mbr=new MenuBar();

setMenuBar(mbr);

Menu edit=new Menu(" do ");

MenuItem copy=new MenuItem("Copy Ctrl+C");

MenuItem zhantie=new MenuItem(" Ctrl+V");

MenuItem exit=new MenuItem("Close Alt+F4");

edit.add(copy);

edit.add(zhantie);

mbr.add(edit);

Menu help=new Menu(" help ");

MenuItem help1=new MenuItem("help theme");

MenuItem about=new MenuItem("about us");

dlog.setAlwaysOnTop(true);

dlog.setResizable(false);

//dlog.setEnabled(false);

s1="学习时兴趣小组做的";

aboutlab.setSize(200, 150);

aboutlab.setText(s1);

dlog.add(aboutlab);

about.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

dlog.setVisible(true);

}

});

dlog.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

dlog.setVisible(false);

}

});

help.add(help1);

help.add(about);

mbr.add(help);

p1.setBackground(Color.lightGray);

p1.setForeground(Color.red);

p2.setBackground(Color.lightGray);

p2.setForeground(Color.blue);

p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 1,1));

p1.add(textfields);

p1.add(buttons1);

buttons1.addActionListener(this);

p2.setLayout(new GridLayout(5, 4, 10, 16));

this.add(p1, BorderLayout.CENTER);

this.add(p2, BorderLayout.SOUTH);

this.setTitle("Play the window");

this.setResizable(false);

this.setAlwaysOnTop(true);

/**textfields.addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e){

char c=e.getKeyChar();

String textfieldsstr=textfields.toString();

switch(c){

case 1:textfields.setText(textfieldsstr+1);break;

case 2:textfields.setText(textfields.toString()+2);break;

case 3:textfields.setText(textfields.toString()+3);break;

case 4:textfields.setText(textfields.toString()+4);break;

case 5:textfields.setText(textfields.toString()+5);break;

case 6:textfields.setText(textfields.toString()+6);break;

case 7:textfields.setText(textfields.toString()+7);break;

case 8:textfields.setText(textfields.toString()+8);break;

case 9:textfields.setText(textfields.toString()+9);break;

case 0:textfields.setText(textfields.toString()+0);break;

case '*':textfields.setText(textfields.toString()+"*");break;

}

/**if(e.getKeyChar()==1){

textfields.setText(textfields.toString()+1);

}

}

});*///未实现键盘监听

Toolkit tk=Toolkit.getDefaultToolkit();

Dimension screenSize = tk.getScreenSize();

screenWidth=screenSize.width;

screenHeight=screenSize.height;

this.setBounds(screenWidth/2-150, screenHeight/2-128, 300, 256);

dlog.setBounds(screenWidth/2-100, screenHeight/2-78, 200, 150);

int i, j = 0, k = 9, r1 = 0, r2 = 0, r3 = 0;

for (i = 0; i < 20; i++) {

j++;

if (j == 4) {

j = 0;

r1++;

Button buttons2_i = new Button();

p2.add(buttons2_i);

buttons2_i.setBackground(Color.cyan);

buttons2_i.addActionListener(this);

switch (r1) {

case 1:

buttons2_i.setLabel("+");

break;

case 2:

buttons2_i.setLabel("-");

break;

case 3:

buttons2_i.setLabel("*");

break;

case 4:

buttons2_i.setLabel("/");

break;

case 5:

buttons2_i.setLabel("=");

break;

}

}// endif

else // else1

{

if (i < 3) {

r2++;

Button buttons2_i = new Button(""); //

p2.add(buttons2_i);

buttons2_i.setBackground(Color.cyan);

buttons2_i.addActionListener(this);

switch (r2) {

case 1:

buttons2_i.setLabel("(");

break;

case 2:

buttons2_i.setLabel(")");

break;

case 3:

buttons2_i.setLabel("sqrt");

break;

}

}// endif

else {

if (i > 16 && i < 19) {

r3++;

Button buttons2_i = new Button("");

p2.add(buttons2_i);

buttons2_i.addActionListener(this);

switch (r3) {

case 1:

buttons2_i.setLabel(".");

break;

case 2:

buttons2_i.setLabel("CE");

break;

}

}// endif

else {

Integer name = new Integer(k);

k--;

s_show = name.toString();

Button buttons2_i = new Button(s_show);

p2.add(buttons2_i);

buttons2_i.addActionListener(this);

}

}

}// end else1

}// end for

s_show = "";

s_go = "";

this.setVisible(true);

}// end Calculator()构造方法

public void actionPerformed(ActionEvent e) {

if (e.getSource() instanceof Button) // if1

{

if (e.getActionCommand() == "CE")// Del

{

s_show = "";

s_go = "";

textfields.setText(s_show);

} else if (e.getActionCommand() == "Backspace") {

if (s_show.equals("")) {

s_go = "";

textfields.setText("");

} else {

if (s_go.length() > 0)

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

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

textfields.setText(s_show);

}

} else if (e.getActionCommand() == ".") {

if (textfields.getText().equals(""))

textfields.setText("0.");

else {

s_show += e.getActionCommand();

s_go += e.getActionCommand();

textfields.setText(s_show);

}

} else {

if (e.getActionCommand() != "=") {

if (e.getActionCommand().equals("sqrt")) {

s_show += e.getActionCommand();

s_go += e.getActionCommand();

textfields.setText(s_show);

s_go = s_go.substring(0, s_go.length() - 3);

} else {

s_show += e.getActionCommand();

s_go += e.getActionCommand();

textfields.setText(s_show);

}

} else {

s_show += e.getActionCommand();

s_go += e.getActionCommand();

textfields.setText(do1.operator(s_go)); // ().toString()

}

}// end else

}// end if1

if(e.getSource()=="copy"){

sedit=textfields.getSelectedText();

StringSelection text=new StringSelection(sedit);

clp.setContents(text, null);

}

/**else if(e.getSource()=="zhantie"){

Transferable contents=clp.getContents(this);//返回表示剪贴板当前内容的 transferable 对象。

DataFlavor flavor=DataFlavor.stringFlavor;//

String ss;

if (contents.isDataFlavorSupported(flavor)) {

try

{

int start=textfields.getSelectionStart();

int end=textfields.getSelectionEnd();

//textfields.replaceRange("",start,end);

textfields.setText(ss);

ss = (String)contents.getTransferData(flavor);

int n=textfields.getCaretPosition();

textfields.insert(ss,n);

}catch(Exception ee){

// TODO: handle exception

}

}

}*///未实现剪贴板粘贴功能

// end actionPerformed()

// end class Calculator

}

public static void main(String args[]) {

Calculator cal = new Calculator();

}

}

求一个简单的JAVA计算器代码

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.TextField;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

public class Calculator {

public static void main(String[] args) {

Frame f = new Frame("Calculator");

final TextField tf = new TextField();

Panel p = new Panel();

f.setLayout(new BorderLayout());

f.add(tf, BorderLayout.NORTH);

f.add(p, BorderLayout.CENTER);

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

Button one = new Button("1");

Button two = new Button("2");

Button three = new Button("3");

Button four = new Button("4");

Button five = new Button("5");

Button six = new Button("6");

Button seven = new Button("7");

Button eight = new Button("8");

Button nine = new Button("9");

Button zero = new Button("0");

Button add = new Button("+");

Button sub = new Button("-");

Button mul = new Button("*");

Button div = new Button("/");

Button decimal = new Button(".");

final Button equal = new Button("=");

p.add(one);

p.add(two);

p.add(three);

p.add(four);

p.add(five);

p.add(six);

p.add(seven);

p.add(eight);

p.add(nine);

p.add(zero);

p.add(add);

p.add(sub);

p.add(mul);

p.add(div);

p.add(decimal);

p.add(equal);

final String[] ary = {"", null, ""};

class NumMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

String num = ((Button)e.getSource()).getLabel().trim();

if(ary[1] == null){

ary[0] = ary[0] + "" + num;

}else{

ary[2] = ary[2] + "" + num;

}

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

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

one.addMouseListener(new NumMouseListener());

two.addMouseListener(new NumMouseListener());

three.addMouseListener(new NumMouseListener());

four.addMouseListener(new NumMouseListener());

five.addMouseListener(new NumMouseListener());

six.addMouseListener(new NumMouseListener());

seven.addMouseListener(new NumMouseListener());

eight.addMouseListener(new NumMouseListener());

nine.addMouseListener(new NumMouseListener());

zero.addMouseListener(new NumMouseListener());

class OperatotrMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(e.getSource() == equal){

try {

long result = calculate(Integer.parseInt(ary[0]), Integer.parseInt(ary[2]), ary[1]);

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

ary[0] = tf.getText();

ary[1] = null;

ary[2] = "";

} catch (Exception e1) {

e1.printStackTrace();

}

}else{

ary[1] = ((Button) e.getSource()).getLabel();

if(ary[0].length() > 0){

ary[2] = "";

}else{

ary[0] = String.valueOf(tf.getText());

}

tf.setText("");

}

}

private long calculate(int i, int j, String operator) throws Exception {

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

return i + j;

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

return i - j;

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

return i * j;

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

return i / j;

}else{

throw new Exception("Invalid operator.(Only +-/* accepted");

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

add.addMouseListener(new OperatotrMouseListener());

mul.addMouseListener(new OperatotrMouseListener());

div.addMouseListener(new OperatotrMouseListener());

sub.addMouseListener(new OperatotrMouseListener());

equal.addMouseListener(new OperatotrMouseListener());

f.addWindowListener(new WindowListener(){

public void windowClosing(WindowEvent e) {

System.exit(0);

}

public void windowActivated(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowOpened(WindowEvent e) {}

});

f.setVisible(true);

f.setBounds(200, 200, 250, 250);

f.validate();

}

}

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class Counter extends WindowAdapter

{

static JFrame f=new JFrame("计算器");

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("除数不能为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编程实现计算器”这个话题的介绍,今天小编就给大家分享完了,如果对你有所帮助请保持对本站的关注!

本文来自作者[骑建立]投稿,不代表小熊号立场,如若转载,请注明出处:https://xx-scm.com/cshi/202604-178882.html

(207)

文章推荐

  • 常识科普“微乐锄大地透视挂视频教程(透视挂真的假的)

    常识科普“微乐锄大地透视挂视频教程网上科普有关“常识科普“微乐锄大地透视挂视频教程”话题很是火热,小编也是针对常识科普“微乐锄大地透视挂视频教程寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,常识科普“微乐锄大地透视挂视频教程这款游戏

    2025年03月15日
    56
  • 3分钟学会“微乐开挂免费软件苹果版下载安装(真的有挂)

    3分钟学会“微乐开挂免费软件苹果版下载安装网上科普有关“3分钟学会“微乐开挂免费软件苹果版下载安装”话题很是火热,小编也是针对3分钟学会“微乐开挂免费软件苹果版下载安装寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,3分钟学会“微乐开

    2025年03月20日
    60
  • 3分钟学会“手机上打牌总是输有什么办法(详细透视开挂教程)

    3分钟学会“手机上打牌总是输有什么办法网上科普有关“3分钟学会“手机上打牌总是输有什么办法”话题很是火热,小编也是针对3分钟学会“手机上打牌总是输有什么办法寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,3分钟学会“手机上打牌总是输有

    2025年03月26日
    67
  • 有没什么关于仙剑的经典文章

    网上科普有关“有没什么关于仙剑的经典文章”话题很是火热,小编也是针对有没什么关于仙剑的经典文章寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。芳心苦·先序物是人非世事休,欲语泪先流!曾几何时,只如一个懵懂小孩,痴迷地安坐一旁,看着你们在宿命路前的徘徊

    2025年04月02日
    264
  • 教程辅助!问鼎娱乐究竟可以开挂吗(透视挂真的假的)

    问鼎娱乐究竟可以开挂吗网上科普有关“问鼎娱乐究竟可以开挂吗”话题很是火热,小编也是针对问鼎娱乐究竟可以开挂吗寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,问鼎娱乐究竟可以开挂吗这款游戏可以开挂的,确实是有挂的,通过微信【】很多玩家在

    2025年04月18日
    66
  • 实测教程“小程序蜀山四川麻将到底有挂”确实真的有挂

    小程序蜀山四川麻将到底有挂网上科普有关“小程序蜀山四川麻将到底有挂”话题很是火热,小编也是针对小程序蜀山四川麻将到底有挂寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,小程序蜀山四川麻将到底有挂这款游戏可以开挂的,确实是有挂的,通过微

    2025年04月19日
    70
  • 推荐一款“非常麻将有挂吗果然有挂”(其实是有挂)

    网上科普有关“非常麻将有挂吗果然有挂”话题很是火热,小编也是针对同城衡阳字牌作弊开挂的方法寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,非常麻将有挂吗果然有挂这款游戏可以开挂的,确实是有挂的,通过微信【游戏】很多玩家在这款游戏中打牌都会

    2025年05月16日
    51
  • 实测辅助”云烟休闲怎么装挂”实测确实有挂

    云烟休闲怎么装挂网上科普有关“云烟休闲怎么装挂”话题很是火热,小编也是针对云烟休闲怎么装挂寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,云烟休闲怎么装挂这款游戏可以开挂的,确实是有挂的,通过微信【】很多玩家在这款游戏中打牌都会发现很

    2025年05月20日
    43
  • 辅助开挂工具“杭州都莱游戏真的有挂吗(透视挂真的假的)

    杭州都莱游戏真的有挂吗网上科普有关“杭州都莱游戏真的有挂吗”话题很是火热,小编也是针对杭州都莱游戏真的有挂吗寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,杭州都莱游戏真的有挂吗这款游戏可以开挂的,确实是有挂的,通过微信【】很多玩家在

    2025年06月29日
    24
  • 实测分析“上嘎海南麻将开挂神器下载(真的有挂)

    网上科普有关“上嘎海南麻将开挂神器下载”话题很是火热,小编也是针对同城衡阳字牌作弊开挂的方法寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。  您好,上嘎海南麻将开挂神器下载这款游戏可以开挂的,确实是有挂的,通过微信【游戏】很多玩家在这款游戏中打牌

    2025年08月18日
    11

发表回复

本站作者后才能评论

评论列表(4条)

  • 骑建立
    骑建立 2026年04月14日

    我是小熊号的签约作者“骑建立”!

  • 骑建立
    骑建立 2026年04月14日

    希望本篇文章《java编程实现计算器》能对你有所帮助!

  • 骑建立
    骑建立 2026年04月14日

    本站[小熊号]内容主要涵盖:国足,欧洲杯,世界杯,篮球,欧冠,亚冠,英超,足球,综合体育

  • 骑建立
    骑建立 2026年04月14日

    本文概览:网上科普有关“java编程实现计算器”话题很是火热,小编也是针对java编程实现计算器寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。i...