Current location - Education and Training Encyclopedia - Graduation thesis - Java calculator course design report
Java calculator course design report
Import java.awt. *;

Import java.awt.event. *;

Import javax.swing. *; //Guide package

Class MyClass extends JFrame.

//Create a MyClass class that inherits the window class of the JFrame framework.

//That is to say, all the functions in JFrame can be realized through MyClass.

{

JLabel a 1=new JLabel ("the first number");

//Create a label that displays the first number.

JLabel a2=new JLabel ("the second number");

JLabel a3=new JLabel ("operation result");

JTextField b 1 = new JTextField(5);

//Create a text box with a default length of 5, which is used to enter the operation number, or it can be blank by default.

JTextField B2 = new JTextField(5);

JTextField B3 = new JTextField(5);

//Create a label to display the operation result, or create a label to display the operation result.

JButton a = new JButton(" plus ");

//Create a button for addition calculation, and add it when clicked.

JButton b = new JButton(" MINUS ");

JButton c = new JButton(" multiply ");

JButton d = new JButton(" divide ");

JPanel jp 1 = new JPanel(); //Create a panel for placing controls.

JPanel jp2 = new JPanel();

JPanel jp3 = new JPanel();

MyClass()// constructor, used to initialize.

{

setLayout(new GridLayout(3, 1)); //Add a layout manager with four rows and four columns.

jp 1 . set layout(new FlowLayout()); //Set the JP 1 panel as a streaming layout manager.

jp 1 . set layout(new FlowLayout());

//Add four controls A 1, B 1, A2 and B2 to the jp 1 panel.

jp 1 . add(a 1);

jp 1 . add(b 1);

jp 1 . add(a2);

jp 1 . add(B2);

jp 1 . add(a3);

//Add four controls A, B, C and D to the jp2 panel.

jp2 . add(a);

jp2 . add(b);

jp2 . add(c);

jp2 . add(d);

jp3 . add(a3);

jp3 . add(B3);

//Add three panels JP 1, JP2 and JP3 to the window.

Add (jp1);

Add (JP3);

Add (jp2);

Object e;

A.addActionListener (new ActionListener ())

//Create an anonymous event listener

{

@ Overlay

public void action performed(action event e){

// TODO automatically generated method stub

double x = double . value of(b 1 . gettext()。 toString());

//Get the first input number and convert it from a string to a double.

double y = double . value of(B2 . gettext()。 toString());

//Get the second input number and convert it from a string to a double.

B3 . settext("+(x+y));

//Display the operation result in the text box b3.

}

});

B.addActionListener (new ActionListener ())

{

@ Overlay

public void action performed(action event e){

// TODO automatically generated method stub

double x = double . value of(b 1 . gettext()。 toString());

double y = double . value of(B2 . gettext()。 toString());

B3 . settext("+(x-y));

}

});

C.addactionlistener (newactionlistener ()//Create an anonymous event listener.

{

@ Overlay

public void action performed(action event e){

// TODO automatically generated method stub

double x = double . value of(b 1 . gettext()。 toString());

double y = double . value of(B2 . gettext()。 toString());

B3 . settext("+(x * y));

}

});

D.addactionlistener (newactionlistener ()//Create an anonymous event listener.

{

@ Overlay

public void action performed(action event e){

// TODO automatically generated method stub

double x = double . value of(b 1 . gettext()。 toString());

double y = double . value of(B2 . gettext()。 toString());

//Because 0 is not divisible, judgment is needed here.

If (y==0)

{

B3 . settext(" error ");

}

other

{

B3 . settext("+(x/y));

}

}

});

//The following are the properties of the setting window.

This.setTitle ("calculator"); //Set the title of the window.

//this.setSize(400,400); //Set the size of the window, or change it to this.pack ().

this . pack();

this . setdefaultcloseoperation(DISPOSE _ ON _ CLOSE); //Set the closing attribute

this . set visible(true); //Set the visibility of the window

}

Public static void main (string [] args)//main function

{

new my class();

}

}