Current location - Education and Training Encyclopedia - Graduation thesis - JAVA training report
JAVA training report
1, training topics and introduction:

Topic: java calculator application

Introduction: The basic functions of calculator can be realized through this program. A program is a subclass of the Frame class. GridLayout is used to lay out components, and buttons and text box components are stored in containers of the Panel class. A large number of else-if statements are also used in the program.

2, design description:

The purpose of the experiment

Through practical training, combined with examples, we can further understand the basic concepts of object-oriented programming, master the basic methods and skills of programming, use packages flexibly, further understand the interface, and master the realization of events in the interface skillfully. Deeply understand and flexibly use the basic characteristics of object-oriented programming, cultivate good programming style, and improve abstract thinking and logical thinking ability.

Second, the experimental content

Write an application to realize a calculator.

Introduction of three programs and their function realization

The program needs to import java.awt.*, java.awt.event.*, java.lang.*, and javax.swing.* package. ① The basic framework of the program is: first define a subclass of Frame class, and then declare the layout of three panels, that is, define three objects of GridLayout class gl 1, gl2 and gl3. Then there are four objects p0, p 1, p2 and p3 that define the container of the Panel class, where p0 is used to store display text box components, p 1 is used to store delete and reset buttons, p3 is used to store number buttons and operation buttons, and p2 is used to store other function buttons. ② The specific implementation process is as follows: (1) The program defines the objects tf 1 of JTextField class, tf2 of TextField class and 26 buttons of button class, initializes each object in the construction method of Counter class, arranges panels appropriately, and adds each button to the corresponding container accordingly, and registers the listener while instantiating all buttons and setting their foreground color.

(2) After the corresponding design of each component is completed, that is, after the visual interface of the program is completed, the internal event process of the program, that is, the realization of the interface, will be realized. A class named Bt is defined to implement this interface. This program implements the specific methods of each button and text box component in the actionPerformed () method. In the program, else-if statement is used to realize button operation events and handle exceptions of events.

3. Program list

Import java.awt. *;

Import java.awt.event. *;

Import java.lang. *;

Import javax.swing. *;

Common class counter extension framework

{

//Declare the layout of three panels

GridLayout gl 1,gl2,gl3

Panel p0, p 1, p2, P3;

JTextField TF 1;

TextField tf2

Buttons b0, b 1, b2, b3, b4, b5, b6, b7, b8, b9, b 10, b 16, b 12, b 13, B.

StringBuffer string; //The character string displayed on the display screen

Double x, y; //x and y are operands.

int z; //Z indicates which operator was clicked. 0 means "+",1 means "-",2 means "*" and 3 means "/"

Static double m; //Memorized numbers

Common counter ()

{

GL 1 = new GridLayout( 1,4, 10,0); //Instantiate the layout of three panels

gl2=new GridLayout(4, 1,0, 15);

gl3=new GridLayout(4,5, 10, 15);

TF 1 = new JTextField(27); //display screen

TF 1 . sethorizontal alignment(JTextField。 Right);

TF 1 . set enabled(false);

TF 1 . settext(" 0 ");

Tf2 = new text field (10); //Displays the memorized index value.

tf2 . set editable(false);

//Instantiate all buttons, set their foreground colors and register listeners.

B0 = New button ("Backspace");

B0 . set foreground(color . red);

B0 . addactionlistener(new Bt());

B 1 = New button ("CE");

b 1 . set foreground(color . red);

b 1 . addactionlistener(new Bt());

B2 = New button ("c");

B2 . set foreground(color . red);

B2 . addactionlistener(new Bt());

B3 = New button ("MC");

B3 . set foreground(color . red);

B3 . addactionlistener(new Bt());

B4 = new button ("Mr");

B4 . set foreground(color . red);

B4 . addactionlistener(new Bt());

B5 = new button ("ms");

b5 . set foreground(color . red);

b5 . addactionlistener(new Bt());

B6 = New button ("m+");

B6 . set foreground(color . red);

B6 . addactionlistener(new Bt());

B7 = New button ("7");

B7 . set foreground(color . blue);

B7 . addactionlistener(new Bt());

B8 = New button ("8");

b8 . set foreground(color . blue);

b8 . addactionlistener(new Bt());

B9 = New button ("9");

B9 . set foreground(color . blue);

B9 . addactionlistener(new Bt());

B 10 = New button ("/");

b 10 . set foreground(color . red);

b 10 . addactionlistener(new Bt());

B 1 1 = New button ("sqrt");

b 1 1 . set foreground(color . blue);

b 1 1 . addactionlistener(new Bt());

B 12 = New button ("4");

b 12 . set foreground(color . blue);

b 12 . addactionlistener(new Bt());

B 13 = New button ("5");

b 13 . set foreground(color . blue);

b 13 . addactionlistener(new Bt());

B 14 = New button ("6");

b 14 . set foreground(color . blue);

b 14 . addactionlistener(new Bt());

B 15 = New button ("*");

b 15 . set foreground(color . red);

b 15 . addactionlistener(new Bt());

B 16 = New button ("%");

b 16 . set foreground(color . blue);

b 16 . addactionlistener(new Bt());

B 17 = New button ("1");

b 17 . set foreground(color . blue);

b 17 . addactionlistener(new Bt());

B 18 = New button ("2");

b 18 . set foreground(color . blue);

b 18 . addactionlistener(new Bt());

B 19 = New button ("3");

b 19 . set foreground(color . blue);

b 19 . addactionlistener(new Bt());

B20 = New button ("-");

b20 . set foreground(color . red);

b20 . addactionlistener(new Bt());

B 2 1 = New button ("1/x");

b 2 1 . set foreground(color . blue);

b 2 1 . addactionlistener(new Bt());

B22 = New button ("0");

b22 . set foreground(color . blue);

b22 . addactionlistener(new Bt());

B23 = New button ("+/-");

b23 . set foreground(color . blue);

b23 . addactionlistener(new Bt());

B24 = New button (".");

b24 . set foreground(color . blue);

b24 . addactionlistener(new Bt());

B25 = New button ("+");

b25 . set foreground(color . red);

b25 . addactionlistener(new Bt());

B26 = New button ("=");

b26 . set foreground(color . red);

b26 . addactionlistener(new Bt());

//instantiate four panels

P0 = new panel ();

P 1 = new panel ();

P2 = new panel ();

P3 = new Panel();

//create an empty stringbuffer area.

str = new string buffer();

//Add components in panel p0 and set their positions and sizes in the frame.

P0 . add(TF 1);

p0.setBounds( 10,25,300,40);

//Add components in panel p 1 and set their positions and sizes in the frame.

p 1 . set layout(GL 1);

p 1 . add(tf2);

p 1 . add(B0);

p 1 . add(b 1);

p 1 . add(B2);

p 1.setBounds( 10,65,300,25);

//Add a component in the panel p2 and set its position and size in the frame.

p2 . set layout(gl2);

p2 . add(B3);

p2 . add(B4);

p2 . add(b5);

p2 . add(B6);

p2.setBounds( 10, 1 10,40, 150);

//Add a component in panel p3 and set its position and size in the frame.

P3 . set layout(gl3); //Set the layout of p3

P3 . add(B7);

P3 . add(b8);

P3 . add(B9);

P3 . add(b 10);

P3 . add(b 1 1);

P3 . add(b 12);

P3 . add(b 13);

P3 . add(b 14);

P3 . add(b 15);

P3 . add(b 16);

P3 . add(b 17);

P3 . add(b 18);

P3 . add(b 19);

P3 . add(b20);

P3 . add(b 2 1);

P3 . add(b22);

P3 . add(b23);

P3 . add(b24);

P3 . add(b25);

P3 . add(b26);

p3.setBounds(60, 1 10,250, 150);

//Set the layout in the frame to be empty and add 4 panels.

set layout(null);

Add (P0);

Add (p1);

Add (p2);

Add (P3);

setResizable(false); //It is forbidden to resize the frame

//Anonymous class closes the window

addWindowListener(new window adapter(){

The public void window is closed (WindowEvent e 1)

{

system . exit(0);

}

});

set background(color . light gray);

setBounds( 100, 100,320,280);

set visible(true);

}

//Construct a listener

Bt class implements ActionListener

{

A public invalid operation has been performed (operation event e2).

{

Try {

If (e2.getsourCE () = = b1)/Select "ce" to clear.

{

TF 1 . settext(" 0 "); //Clear the display.

str . setlength(0); //Clear the stringbuffer area to prepare for receiving new input operands.

}

Else if(e2.getSource()==b2)// Select "c" to clear.

{

TF 1 . settext(" 0 "); //Clear the display.

str . setlength(0);

}

Else if(e2.getSource()==b23)// Click "+/-"to select whether the input operand is positive or negative.

{

x = double . parse double(TF 1 . gettext()。 trim());

TF 1 . settext("+(-x));

}

Else if(e2.getSource()==b25)// Click the plus button to get the values of x and z, and clear the value of y..

{

x = double . parse double(TF 1 . gettext()。 trim());

str . setlength(0); //Clear the buffer to receive another new operand.

y = 0d

z = 0;

}

Else if(e2.getSource()==b20)// Click the minus button to get the values of x and z, and clear the value of y..

{

x = double . parse double(TF 1 . gettext()。 trim());

str . setlength(0);

y = 0d

z = 1;

}

Elseif (e2.getsource () = = b15)//Click the multiply button to get the value of x and the value of z, and clear the value of y..

{

x = double . parse double(TF 1 . gettext()。 trim());

str . setlength(0);

y = 0d

z = 2;

}

Elseif (e2.getsource () = = b10)//Click the divide button to get the value of x and the value of z, and leave the value of y blank.

{

x = double . parse double(TF 1 . gettext()。 trim());

str . setlength(0);

y = 0d

z = 3;

}

Else if(e2.getSource()==b26)// Click the equal sign button to output the calculation result.

{

str . setlength(0);

Switch (z)

{

Case 0: TF1.settext ("+(x+y)); Break;

case 1:TF 1 . settext("+(x-y)); Break;

Case 2: TF1.settext ("+(x * y)); Break;

Case 3: TF1.settext ("+(x/y)); Break;

}

}

Else if(e2.getSource()==b24)// Click the "."button to enter a decimal.

{

if(tf 1.getText()。 Trim (). indexOf(“.”))! =-1)/Determines whether the string contains decimal points.

{

}

Otherwise//If you don't count, it's small.

{

If (tf 1.gettext()。 Trim (). Equals(" 0 ")// If it is initially displayed as 0.

{

str . setlength(0);

TF 1 . settext((str . append(" 0 "+E2 . getactioncommand()))。 toString());

}

Elseif (tf 1.gettext()。 Trim (). Equals(" ")// If it is initially empty, nothing will be done.

{

}

other

{

TF 1 . settext(str . append(E2 . getactioncommand())。 toString());

}

}

y = 0d

}

Elseif (e2.getsource () = = b11)/Find the square root.

{

x = double . parse double(TF 1 . gettext()。 trim());

Tf 1.setText ("abnormal number format");

if(x & lt; 0)

Tf 1.setText ("negative numbers have no square root");

other

TF 1 . settext("+math . sqrt(x));

str . setlength(0);

y = 0d

}

Elseif (e2.getsource () = = b16)//Click the% button.

{

x = double . parse double(TF 1 . gettext()。 trim());

TF 1 . settext("+(0.0 1 * x));

str . setlength(0);

y = 0d

}

Elseif (e2.getsource () = = b 21)/Click "1/X" button.

{

x = double . parse double(TF 1 . gettext()。 trim());

If (x==0)

{

Tf 1.setText ("Divider cannot be zero ");

}

other

{

TF 1 . settext("+( 1/x));

}

str . setlength(0);

y = 0d

}

Else if(e2.getSource()==b3)//MC is to clear the memory.

{

m = 0d

tf2 . settext(" ");

str . setlength(0);

}

Else if(e2.getSource()==b4)//MR refers to the called storage data.

{

if(tf2.getText()。 trim()! = "")//Memory number

{

TF 1 . settext("+m);

}

}

Else if(e2.getSource()==b5)//MS is the data stored and displayed.

{

m = double . parse double(TF 1 . gettext()。 trim());

tf2 . settext(" M ");

TF 1 . settext(" 0 ");

str . setlength(0);

}

Else if(e2.getSource()==b6)//M+ To add the displayed number with the stored data, click MR to view the new number.

{

m = m+double . parse double(TF 1 . gettext()。 trim());

}

Else// Select another button.

{

If(e2.getSource()==b22)// If the number key "0" is selected,

{

If (tf 1.gettext()。 Trim (). Equals(" 0 ")// If zero is displayed, nothing is done.

{

}

other

{

TF 1 . settext(str . append(E2 . getactioncommand())。 toString());

y = double . parse double(TF 1 . gettext()。 trim());

}

}

Else if(e2.getSource()==b0)// The "Backspace" button is selected.

{

If (! Tf 1.gettext()。 Trim (). Equals(" 0 ")// If the display is not zero.

{

if(str.length()! = 1)

{

TF 1 . settext(str . delete(str.length()- 1,str . length())。 toString()); //A string out-of-bounds exception may be thrown.

}

other

{

TF 1 . settext(" 0 ");

str . setlength(0);

}

}

y = double . parse double(TF 1 . gettext()。 trim());

}

Else// Other numeric keys

{

TF 1 . settext(str . append(E2 . getactioncommand())。 toString());

y = double . parse double(TF 1 . gettext()。 trim());

}

}

}

catch(NumberFormatException e){

Tf 1.setText ("abnormal number format");

}

catch(StringIndexOutOfBoundsException e){

Tf 1.setText ("String index out of bounds");

}

}

}

Public static void main(String args[])

{

new Counter();

}

}

4. Debugging results

5. Internship experience