<tfoot id='qv7xk7ts'></tfoot>
<i id='o75eubpx'><tr id='cw9wr619'><dt id='kebwgvo7'><q id='bjq2ve6g'><span id='hst5p8qr'><b id='owicyhzp'><form id='x3gugtct'><ins id='8rmjfew7'></ins><ul id='4y8mmuza'></ul><sub id='o39402le'></sub></form><legend id='qbokru5s'></legend><bdo id='o19be20x'><pre id='rd0v412t'><center id='0o0bw47k'></center></pre></bdo></b><th id='6sqwj8ge'></th></span></q></dt></tr></i><div id='bpiykgz3'><tfoot id='vjyhl4yq'></tfoot><dl id='zh4tnvbn'><fieldset id='pxmbwk4v'></fieldset></dl></div>
  • <small id='1cwwapvc'></small><noframes id='qtd1fgun'>

    1. <legend id='7mkaj6po'><style id='nrte5ohi'><dir id='3kl4ge8j'><q id='3ijncabp'></q></dir></style></legend>
        <bdo id='skzkrvec'></bdo><ul id='zw6ii8to'></ul>

        欢迎来到入门教程网!

        Java编程

        当前位置:主页 > 软件编程 > Java编程 >

        计算器java源代码 计算器编程代码javagui

        来源:本站原创|时间:2023-04-04|栏目:Java编程|点击:

        用JAVA编写的科学计算器源代码

        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代码

        import java.awt.Color;

        import java.awt.Font;

        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.JLabel;

        import javax.swing.JPanel;

        import javax.swing.JTextField;

        import javax.swing.WindowConstants;

        import javax.swing.border.LineBorder;

        class Normal{

        double i,j;

        public Normal(double num1,double num2){

        i=num1;

        j=num2;

        }

        public double puls(){

        return i+j;

        }

        public double subtract(){

        return i-j;

        }

        public double multiply(){

        return i*j;

        }

        public double divide(){

        return i/j;

        }

        public double surpuls(){

        return i%j;

        }

        }

        class scientific extends Normal{

        public scientific(int num1, int num2) {

        super(num1, num2);

        }

        }

        public class calc extends JFrame{

        public static void main(String[] args) {

        viewNormal VN= new viewNormal("normal");

        }

        }

        class viewNormal extends JFrame implements ActionListener{

        JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));

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

        JLabel jl;

        JButton[] jb;

        JButton jbs,jbo,jba,jbb,jbc,jby;

        StringBuffer sb = new StringBuffer();

        Normal normal;

        int dot=0;

        double fnum=0;

        double lnum=0;

        double result;

        String sign=null;

        public viewNormal(String title){

        setTitle(title);

        setLayout(null);

        setVisible(true);

        setBounds(200,200,305,350);

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        jb= new JButton[12];

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

        jb[i]=new JButton(""+(i+1));

        jp1.add(jb[i]);

        jb[i].addActionListener(this);

        }

        jb[9]=new JButton(".");

        jb[10]=new JButton("0");

        jb[11]=new JButton("=");

        jb[9].addActionListener(this);

        jb[10].addActionListener(this);

        jb[11].addActionListener(this);

        jp1.add(jb[9]);

        jp1.add(jb[10]);

        jp1.add(jb[11]);

        jp1.setBounds(10, 100, 200, 200);

        jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");

        jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");

        jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);

        jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);

        //jp2.add(jby);

        jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);

        jp2.setBounds(215, 100, 70, 200);

        jl= new JLabel("0",JLabel.RIGHT);

        jl.setFont(new Font("Batang",Font.BOLD, 20));

        jl.setBorder(new LineBorder(Color.black,2));

        jl.setBackground(Color.white);

        jl.setBounds(10, 40, 275, 50);

        jl.setOpaque(true);

        add(jl);

        add(jp1);

        add(jp2);

        }

        //+

        public void sum(){

        lnum=Double.parseDouble(sb.toString());

        normal=new Normal(fnum,lnum);

        fnum=normal.puls();

        result=fnum;

        }

        //-

        private void sub() {

        System.out.println(sb.toString());

        lnum=Double.parseDouble(sb.toString());

        normal=new Normal(fnum,lnum);

        fnum=normal.subtract();

        result=fnum;

        }

        //*

        private void mul() {

        lnum=Double.parseDouble(sb.toString());

        normal=new Normal(fnum,lnum);

        fnum=normal.multiply();

        result=fnum;

        }

        // /

        private void div() {

        lnum=Double.parseDouble(sb.toString());

        normal=new Normal(fnum,lnum);

        fnum=normal.divide();

        result=fnum;

        }

        //%

        private void sur() {

        lnum=Double.parseDouble(sb.toString());

        normal=new Normal(fnum,lnum);

        fnum=normal.surpuls();

        result=fnum;

        }

        // =

        private void same(){

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

        sum();

        }

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

        sub();

        }

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

        mul();

        }

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

        div();

        }

        if(sign.equals("%")){

        sur();

        }

        }

        //result

        public void Result(){

        if(result%1!=0)

        jl.setText(""+result);

        else

        {

        int i=(int)result;

        jl.setText(""+i);

        }

        }

        @Override

        public void actionPerformed(ActionEvent e) {

        //System.out.println(sb.toString());

        // 1~9

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

        if(e.getSource()==jb[i]!sb.toString().equals("0")){

        sb.append(jb[i].getText());

        jl.setText(sb.toString());

        }

        else if(e.getSource()==jb[i]sb.toString().equals("0")){

        int d=sb.length();

        sb.delete(0, d);

        sb.append(jb[i].getText());

        jl.setText(sb.toString());

        }

        }

        // 0

        if(e.getSource()==jb[10]!sb.toString().equals("0")){

        sb.append(jb[10].getText());

        jl.setText(sb.toString());

        }

        // .

        if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){

        dot++;

        sb.append(jb[9].getText());

        jl.setText(sb.toString());

        }

        // =

        if(e.getSource()==jb[11]!sb.toString().equals("")){

        same();

        Result();

        int d=sb.length();

        sb.delete(0, d);

        dot=0;

        }

        // +

        if(e.getSource()==jbs!sb.toString().equals("")){

        if(sign!="+"sign!=null)

        same();

        else

        sum();

        sign ="+";

        Result();

        int d=sb.length();

        sb.delete(0, d);

        dot=0;

        }

        //-

        if(e.getSource()==jbo!sb.toString().equals("")){

        if(fnum==0)

        fnum=2*Double.parseDouble(sb.toString());

        if(sign!="-"sign!=null)

        same();

        else

        sub();

        sign ="-";

        Result();

        int d=sb.length();

        sb.delete(0, d);

        dot=0;

        }

        //*

        if(e.getSource()==jba!sb.toString().equals("")){

        if(fnum==0)

        fnum=1;

        if(sign!="*"sign!=null)

        same();

        else

        mul();

        sign ="*";

        Result();

        int d=sb.length();

        sb.delete(0, d);

        dot=0;

        }

        // /

        if(e.getSource()==jbb!sb.toString().equals("")){

        if(fnum==0)

        fnum=Math.pow(Double.parseDouble(sb.toString()),2);

        if(sign!="/"sign!=null)

        same();

        else

        div();

        sign ="/";

        Result();

        int d=sb.length();

        sb.delete(0, d);

        dot=0;

        }

        //%

        // if(e.getSource()==jby!sb.toString().equals("")){

        // if(fnum==0){

        // fnum=Double.parseDouble(sb.toString());

        // result=fnum;

        // }

        // else {

        // if(sign!="%"sign!=null)

        // same();

        // else{

        // lnum=Double.parseDouble(sb.toString());

        // normal=new Normal(fnum,lnum);

        // fnum=normal.surpuls();

        // result=fnum;

        // }

        // }

        // sign ="%";

        // Result();

        // int d=sb.length();

        // sb.delete(0, d);

        // dot=0;

        // }

        //clear

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

        int d=sb.length();

        sb.delete(0, d);

        jl.setText("0");

        dot=0;

        fnum=0;

        lnum=0;

        sign=null;

        }

        }

        }

        class viewScientific extends viewNormal{

        public viewScientific(String title){

        super(title);

        setBounds(200,200,800,500);

        }

        }

        //等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。

        如何用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();

        }

        }

      1. <i id='z69shirz'><tr id='lg26mjsy'><dt id='zfa2gqiy'><q id='7urbdmgm'><span id='5fq7dhoz'><b id='wswaykmn'><form id='nldkrdx4'><ins id='v3v7fpbw'></ins><ul id='glz8j6vz'></ul><sub id='4tfby0qx'></sub></form><legend id='s6b89bk7'></legend><bdo id='ww3wwkri'><pre id='wouthrtr'><center id='dgo4jfst'></center></pre></bdo></b><th id='3wi0y6nd'></th></span></q></dt></tr></i><div id='9uibwz0r'><tfoot id='k064u8z1'></tfoot><dl id='kqfky1hs'><fieldset id='jeey19jv'></fieldset></dl></div>
        • <legend id='x5x3ltby'><style id='or28qb9c'><dir id='y2ck0arq'><q id='yp5lh2t0'></q></dir></style></legend>

            <small id='pe58duh8'></small><noframes id='voij8yyt'>

            <tfoot id='pg89manb'></tfoot>
              <bdo id='66kqbw98'></bdo><ul id='nzul9b01'></ul>

                  <tbody id='bim7gjr3'></tbody>
                • 上一篇:再js中写java代码 js中怎么写java代码

                  栏    目:Java编程

                  下一篇:没有了

                  本文标题:计算器java源代码 计算器编程代码javagui

                  本文地址:https://www.xiuzhanwang.com/a1/Javabiancheng/17246.html

                  网页制作CMS教程网络编程软件编程脚本语言数据库服务器

                  如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

                  联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

                  Copyright © 2002-2020 脚本教程网 版权所有

                  <i id='6sadutj9'><tr id='tviempzf'><dt id='dx63euln'><q id='7hf48ok5'><span id='240jvt6u'><b id='90n55asx'><form id='ojsofqp1'><ins id='d4fnrzkk'></ins><ul id='g2v5x0qy'></ul><sub id='79ng6lcl'></sub></form><legend id='5bfaxi3j'></legend><bdo id='jgdu8nc3'><pre id='1nl7pp3z'><center id='ctmgypmh'></center></pre></bdo></b><th id='sq5av7ap'></th></span></q></dt></tr></i><div id='vxg76bru'><tfoot id='lbingrbq'></tfoot><dl id='veb9c0go'><fieldset id='0yq9pr5i'></fieldset></dl></div>
                • <small id='fh666r0n'></small><noframes id='2dnb6hhj'>

                    <bdo id='cfwfgego'></bdo><ul id='ceedj57j'></ul>

                  <legend id='z38gzmzt'><style id='tjjy3z5o'><dir id='cyj3xi2o'><q id='0jgivwqp'></q></dir></style></legend>

                    1. <tfoot id='dkz11ne9'></tfoot>