Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Seek a java calculator program annotation!
Seek a java calculator program annotation!
Package calculation test;

Import java.awt.borderlayout;

Import java.awt.color;

Import java.awt.container;

Import java.awt.flowlayout;

Import java.awt.font;

Import java.awt.gridlayout;

Import java.awt.scrollpane;

Import java.awt.event.actionevent;

Import java.awt.event.actionlistener;

Import java.text.decimalformat;

Import javax.swing.jbutton;

Import javax.swing.jframe;

Import javax.swing.jmenu;

Import javax.swing.jmenubar;

Import javax.swing.j menuitem;

Import javax.swing.jooptionpane;

Import javax.swing.jpanel;

Import javax.swing.jscrollpane;

Import javax.swing.jtextarea;

Import javax.swing.jtextfield;

Import javax.swing.text.styleconstants;

Import javax.swing.text.stylecontext;

Import org.w3c.dom.text;

Import com.sun.org.apache.bcel.internal.generic.jsrinstruction;

The public class Calc extends JFrame to realize ActionListener{

public final int max = 8; //Defines the maximum number of characters to enter in the text box.

String replication;

Boolean input;

int key = - 1,prekey =- 1;

double vard,answerd

String curDate =

Double opate1= 0.0;

Double opate2 = 0.0

Decimal format df;

JPanel ButtonPanel,okPanel

JButton JB[];

JTextField jtf

JScrollPane jsp

Jia, Jane, Cheng, Chu, Dian,;

JMenuBar jmb

JMenu bjM,ckM,bzM

JTextArea jta

JMenuItem copyM,pasteM,closeM,helpM,gyM

JButton b 1;

Boolean begins;

Public Computing () {

enter = true

start = true

Df = new DecimalFormat("0. #########");

Container c = getcontentpane ();

//* * Here is the menu and so on.

jmb = new JMenuBar();

BjM = new JMenu(" Edit(e)");

CkM = new JMenu(" View(v)");

BzM = new JMenu(" help(h)");

CopyM = new JMenuItem(" copy(c)");

PasteM = new JMenuItem(" paste(v)");

CloseM = new JMenuItem(" close(e)");

HelpM = new JMenuItem ("help topic");

GyM = new JMenuItem ("about calculator");

jmb . add(bjM);

jmb . add(ckM);

jmb . add(bzM);

bjm . add(copyM);

bjm . add(pasteM);

ckm . add(closeM);

bzm . add(helpM);

BzM.add (gym);

setJMenuBar(jmb); //Show menu bar

//* * The above are menu classes.

//* * The following register menu listeners.

copym . addactionlistener(this);

pastem . addactionlistener(this);

closem . addactionlistener(this);

helpm . addactionlistener(this);

gym . addactionlistener(this);

//* * Register the menu listener above.

jtf = new JTextField(35);

jtf.setText("0。" );

jtf . set editable(false);

//jtf . setcomponentdorientation(Java . awt . componentdorientation . right _ TO _ LEFT); //Set the text box from right to left.

jtf . sethorizontal alignment(JTextField。 Right); //The effect is the same as above.

jtf . settext(" "); //Set to retrieve each time.

//* * The following are the button settings.

JB = new JButton[ 10];

Jia = new JButton("+");

Jian = new JButton("-");

cheng = new JButton(" * ");

Chu = new JButton(" \ \ ");

dian = new JButton(" . ");

Deng Yu = New JButton ("=");

Jia . addactionlistener(this);

Jian . addactionlistener(this);

cheng . addactionlistener(this);

Chu . addactionlistener(this);

Dian . addactionlistener(this);

dengyu . addactionlistener(this);

button panel = new JPanel();

for(int I = 0; I & ltjb.lengthi++){

JB[I]= new JButton(integer . tostring(I)); //Realize the conversion from plastic to character string.

jb[i]。 SetForeground (new color (99,00,00));

jb[i]。 addActionListener(calc . this); //Register a listener for each button.

}

button panel . set layout(new GridLayout(4,4)); //Set the grid layout of 4 rows and 4 columns.

button panel . add(JB[ 1]);

button panel . add(JB[2]);

button panel . add(JB[3]);

ButtonPanel.add (Jia);

button panel . add(JB[4]);

button panel . add(JB[5]);

button panel . add(JB[6]);

button panel . add(Jian);

button panel . add(JB[7]);

button panel . add(JB[8]);

button panel . add(JB[9]);

ButtonPanel.add (process);

button panel . add(JB[0]);

button panel . add(Dian);

ButtonPanel.add (Deng Yu);

button panel . add(Chu);

//* * The above is the button setting.

//* * The following are help topics.

Jta = new JTextArea(5,10);

JSP = new JScrollPane(JTA);

JTA . set editable(false);

Jta.setFont (new font ("italic", font. Bold, 20));

Jta.append ("Perform simple calculation \ n");

Jta.append(" 1。 Type the first number of the calculation. \ n ");

Jta.append("2。 Click+to add,-to subtract, * to multiply or/and divide. \ n ");

Jta.append("3。 Type the next number to calculate. \ n ");

Jta.append("4。 Enter all remaining operators and numbers. \ n ");

Jta.append("5。 Click "=". \ n ");

//* * These are the help topics.

//* * The following has been reset.

ok panel = new JPanel();

b 1 = new JButton(" C ");

ok panel . add(b 1);

//* * The above has been reset.

b 1 . addactionlistener(this); //Register the Listener

c.add(jtf,BorderLayout。 North);

c.add(ButtonPanel,BorderLayout。 Center);

c.add(okPanel,BorderLayout。 South);

setDefaultCloseOperation(JFrame。 EXIT _ ON _ CLOSE);

}

public void action performed(action event e){

object temp = e . getsource();

if(temp = = Dian & amp; & ampenter == true){

Boolean isDot = false

if(jtf.getText()。 length()==0)

isDot = true

for(int I = 0; I & ltjtf.getText (). Length (); i++){

If ('.'== jtf.getText (). charAt(i)){

system . out . println(I);

isDot = true

Break;

}

}

if(isDot==false)

jtf.setText(jtf.getText() +"。" );

}

if(jtf.getText()。 length()==max){

Jtf.setText ("Input exceeds the maximum length");

enter = false

}

If((temp == Jia || temp == Jane || temp == Cheng|| temp = = Chu)& &

if(temp == jia){

Switch (pre-key) {

Case 0:

answerd+= double . parse double(jtf . gettext()); //Converts a string value to a double-precision value and returns a new double-precision value.

Break;

Case 1:

answerd-= double . parse double(jtf . gettext());

Case 2:

answerd * = double . parse double(jtf . gettext());

Case 3:

if(double . parse double(jtf . gettext())= = 0){

Jtf.setText ("Divider cannot be zero");

enter = false

}

other

answerd/= double . parse double(jtf . gettext());

Break;

Default value:

answerd = double . parse double(jtf . gettext());

system . out . println(answerd);

Break;

}

jtf . settext(" ");

pre key = key = 0;

}

if(temp==jian){

Switch (pre-key) {

Case 0:

answerd+= double . parse double(jtf . gettext());

Break;

Case 1:

answerd-= double . parse double(jtf . gettext());

Case 2:

answerd * = double . parse double(jtf . gettext());

Case 3:

if(double . parse double(jtf . gettext())= = 0){

Jtf.setText ("Divider cannot be zero");

enter = false

}

other

answerd/= double . parse double(jtf . gettext());

Break;

Default value:

answerd = double . parse double(jtf . gettext());

Break;

}

jtf . settext(" ");

prekey = key = 1;

}

if(temp==cheng){

Switch (pre-key) {

Case 0:

answerd+= double . parse double(jtf . gettext());

Break;

Case 1:

answerd-= double . parse double(jtf . gettext());

Case 2:

answerd * = double . parse double(jtf . gettext());

Case 3:

if(double . parse double(jtf . gettext())= = 0){

Jtf.setText ("Divider cannot be zero");

enter = false

}

other

answerd/= double . parse double(jtf . gettext());

Break;

Default value:

answerd = double . parse double(jtf . gettext());

Break;

}

jtf . settext(" ");

prekey = key = 2;

}

if(temp==chu){

Switch (pre-key) {

Case 0:

answerd+= double . parse double(jtf . gettext());

Break;

Case 1:

answerd-= double . parse double(jtf . gettext());

Case 2:

answerd * = double . parse double(jtf . gettext());

Case 3:

if(double . parse double(jtf . gettext())= = 0){

Jtf.setText ("Divider cannot be zero");

enter = false

}

other

answerd/= double . parse double(jtf . gettext());

Break;

Default value:

answerd = double . parse double(jtf . gettext());

Break;

}

jtf . settext(" ");

prekey = key = 3;

}

}

If(temp = = Deng Yu & Ampenter = = true) {

if(prekey==5){

if(key==0){

answerd+= vard;

jtf . settext(df . format(answerd)); //Format a double value to generate a string.

}

if(key== 1){

answerd-= vard;

jtf . settext(df . format(answerd));

}

if(key==2){

answerd * = vard

jtf . settext(df . format(answerd));

}

if(key==3){

if(double . parse double(jtf . gettext())= = 0){

Jtf.setText ("Divider cannot be 0 ");

enter = false

} Otherwise {

answerd/= vard;

jtf . settext(df . format(answerd));

}

}

} Otherwise {

vard = double . parse double(jtf . gettext());

system . out . println(vard);

if(key==0){

prekey =- 1;

answerd+= double . parse double(jtf . gettext());

jtf . settext(df . format(answerd));

}

if(key== 1){

prekey =- 1;

answerd-= double . parse double(jtf . gettext());

jtf . settext(df . format(answerd));

}

if(key==2){

prekey =- 1;

answerd * = double . parse double(jtf . gettext());

jtf . settext(df . format(answerd));

}

if(key==3){

prekey =- 1;

if(double . parse double(jtf . gettext())= = 0){

Jtf.setText ("Divider cannot be 0 ");

enter = false

} Otherwise {

answerd/= double . parse double(jtf . gettext());

jtf . settext(df . format(answerd));

}

}

}

prekey = 5;

}

if(temp==b 1){

jtf . settext(" ");

enter = true

}

//Convert buttons into strings one by one.

for(int I = 0; I & ltjb.lengthi++){

if(temp = = JB[I]& amp; & ampenter==true){

jtf . settext(jtf . gettext()+integer . tostring(I));

}

}

//* * The following events are executed for MemuItem.

if(temp==copyM){

copy = jtf . gettext();

}

if(temp==pasteM){

Jtf.setText (copy);

}

if(temp==closeM){

dispose();

system . exit(0);

}

if(temp==gyM){

//joptionpane . showmessagedialog(gyM,JSP);

joptionpane . showmessagedialog(gyM,JSP);

}

if(temp == helpM){

Joptionpane。 The display message dialog box (helpm, "Calculator") can be used to complete any standard operation, which is usually done with the help of a handheld calculator. \ nCalculator can be used for basic arithmetic operations, such as addition and subtraction. \n It also has the function of scientific calculator, such as logarithmic operation and factorial operation. " );

}

//* * The above events are aimed at MemuItem.

}

Public static void main(String[] args) {

Calc cc = new Calc();

Cc.setTitle ("calculator v1.0");

cc.setSize(400,400);

cc.setBounds(300,300,300,300);

cc . set visible(true);

}

}