Java Simple Calculator

Source: Internet
Author: User

Write it and write it and forget it. By the way read the book haha ~ ~
Window:

Import java.awt.*;p Ublic class Window {public static void main (String args[]) {windowinit win=new windowinit (); Container Con=win.getcontentpane (); Con.setbackground (Color.green);}}


Windowinit:

import java.awt.*;import javax.swing.*;p ublic class Windowinit extends jframe{ JTextField Input1,input2; JComboBox Choicefuhao; JButton button; JTextArea Showarea;operatorlistener operator; Computerlistener computer;public Windowinit () {init (); SetBounds (100,100,429,329); Settitle ("calculator"); setvisible (True) ; Setdefaultcloseoperation (jframe.exit_on_close);} void Init () {setlayout (New FlowLayout ()); Input1=new JTextField (5); input2=new JTextField (5); Button=new JButton ("OK"); Choicefuhao=new JComboBox (); Choicefuhao.additem ("Please select symbol") Choicefuhao.additem ("+"); Choicefuhao.additem ("-"); Choicefuhao.additem ("*"); Choicefuhao.additem ("/"); showarea=new JTextArea (9,36); operator=new OperatorListener (); Computer=new Computerlistener (); Operator.setchoicefuhao (Choicefuhao); Operator.setworktogether (computer); COMPUTER.SETINPUT1 (INPUT1); Computer.setinput2 (INPUT2); Computer.settextarea (Showarea); Button.addactionlistener ( computer);//feel that you should also add 
                  Input1.addactionlistener,input2.addactionlistenerchoicefuhao.additemlistener (operator); add (input1); Add ( Choicefuhao); add (input2); Add (button); add (Showarea);}}


Operatorlistener:

Import javax.swing.*;
Import java.awt.event.*;
public class Operatorlistener implements itemlistener{
JComboBox Choicefuhao;
Computerlistener Worktogether;
public void Setchoicefuhao (JComboBox e) {
Choicefuhao=e;
}
public void Setworktogether (Computerlistener e) {
Worktogether=e;
}
public void itemstatechanged (ItemEvent e) {
String Fuhao=choicefuhao.getselecteditem (). toString ();
Worktogether.setfuhao (Fuhao);
}
}
Computerlistener:

Import Java.awt.event.*;import javax.swing.*;p ublic class Computerlistener implements Actionlistener{jtextfield Input1,input2; JButton button; JTextArea Showarea; String fuhao;public void SetInput1 (JTextField e) {input1=e;} public void SetInput2 (JTextField e) {input2=e;} public void Setfuhao (String e) {fuhao=e;} public void Settextarea (JTextArea e) {showarea=e;} public void actionperformed (ActionEvent e) {double num1=double.parsedouble (Input1.gettext ());d ouble num2= Double.parsedouble (Input2.gettext ());d ouble result=0;if (fuhao.equals ("+")) Result=num1+num2;else if (Fuhao.equals ( "-")) Result=num1-num2;else if (Fuhao.equals ("*")) Result=num1*num2;else Result=num1/num2;showarea.append (num1+ fuhao+num2+ "=" +result+ "\ n");}}


This is the result of the operation:

Java Simple Calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.