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