Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. Lang. *;
import javax.swing.*;
import java.util.*;
Public class CalculatorA {
Private JFrame jf.
Private JButton [] JBS;
Private JTextField JTF;
Private JButton clear;
Private double num1, num2 jieguo;
Private char c;
/* *
* constructor instantiated attribute
*
*/
Public CalculatorA () {
Jf=new JFrame (" my calculator v1.0 ");
JTF=new JTextField (20);
The clear=new JButton (" the clear ");
JBS=new JButton [16].
String STR="123 + 456-0. 789 */=";
for(int i=0; i
}
init();
AddEventHandler ();
//the setFont ();
//setColor ();
ShowMe ();
}
/* *
* the graphical interface layout
*
*/
Public void init () {
//jf. SetLayout (manager)
JPanel jp1=new JPanel ();
Jp1. Add (JTF);
JPanel jp2=new JPanel ();
Jp2. SetLayout (new GridLayout (4, 4));
for(int i=0; i<16. I++) {
Jp2. Add (JBS [I]);
}
JPanel jp3=new JPanel ();
Jp3. Add (clear);
Jf. Add (jp1, BorderLayout. NORTH);
Jf. Add (jp2, BorderLayout. CENTER);
Jf. Add (jp3, BorderLayout. SOUTH);
}
Public void setFont () {
}
Public void setColor () {
}
Public void addEventHandler () {
ActionListener lis=new ActionListener () {
Public void actionPerformed (an ActionEvent e) {
//TODO Auto - generated method stub
JButton jb=(JButton) um participant etSource ();
//the String key=(String) um participant etActionCommand ();
String STR=jb. GetText (). The trim ();//remove the leading and trailing Spaces of the string!
//String str2=um participant etActionCommand (). The trim ();//returns the event source text content
If (" 0123456789 ". IndexOf (STR)!=1) {//if a number or a period
JTF. SetText (JTF. GetText () + STR);
return;
}
If (" + - */". IndexOf (STR)!=1) {
Num1=Double. ParseDouble (JTF getText ());
JTF. SetText (" ");
C=STR. CharAt (0);
JTF. SetText (" ");
return ;
}
If (STR) equals ("=")) {
Num2=Double. ParseDouble (JTF getText ());
//JTF setText (" ");
The switch (c) {
Case: '+' jieguo=num1 + num2; break;
Case '-' : jieguo=num1 - num2; break;
Case: '*' jieguo=num1 * num2; break;
Case '/' : jieguo=num1/num2; break;
}
JTF. SetText (Double. ToString (jieguo));
return;
}
If (um participant etActionCommand (.) the equals (" the clear ")) {
JTF. SetText (" ");
return;
}
}
};
for(int i=0; i
[I] JBS addActionListener (lis);
}
The clear addActionListener (lis);
}
Public void showMe () {
Jf. Pack ();
Jf. SetVisible (true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Public static void main (String [] args) {
New CalculatorA ();
}
}