Java language program design

xiaoxiao2021-03-06  34

Design Title 1: Design the calculator program for the GUI interface, the user can enter the value of the calculated value by the mouse, plus, minus, multiplied, and divide the mixed operation.

This topic is through:

Mouse Click to input an integer by clicking various digital buttons; input arithmetic operation by clicking the operation symbol button; by clicking the Calculator panel Click the CE button to clear the input.

Program output

----

Panel output or output error information (when the system is wrong). // Wang Rui's MycalculatorImport java.awt. *;

Import java.awt.event. *;

Public Class Mycalculator Extends WINDOWAPTER IMPLEments ActionListener {

PRIVATE FRAME F;

Private Button B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,

B13, B14, B15, B16, B17, B18, B19;

Private textfield F1;

Private Double ANS = 0; // Temporary storage results, the result is finally final

Private double n1 = 0; // Defined to store the number to be entered, the initial value is no input number

Private int ob = 0; // At the beginning of the first value, no operation

Private boolean Hasop = false; // Initial value is not pressed

Private Boolean Dot = false; // Defined as a calculation of the small number of calculations, the initial value is no size

Public static void main (string args []) {

Mycalculator be = new mycalculator ();

BE.GO ();

}

Public void Go () {

f = new frame ("Wang Rui's calculator");

F.setLayout (New GridLayout (4, 4));

F1 = new textfield (", 10);

B1 = New Button ("1");

B2 = New Button ("2");

B3 = New Button ("3");

B4 = New Button ("CE");

B5 = New Button ("4");

B6 = New Button ("5");

B7 = New Button ("6");

B8 = New Button (" ");

B9 = New Button ("7");

B10 = New Button ("8");

B11 = New Button ("9");

B12 = New Button ("-");

B13 = new button ("*");

B14 = New Button ("0");

B15 = New Button (".");

B16 = New Button ("/");

B17 = New Button ("=");

B18 = New button (");

B19 = New Button (")");

F.Add (f1);

F.Add (b1);

F.Add (b2);

F.Add (b3);

F.Add (b4);

F.Add (b5);

F.Add (b6);

F.Add (b7); f.Add (b8);

F.Add (b9);

F.Add (b10);

F.Add (b11);

F.Add (b12);

F.Add (b13);

F.Add (b14);

F.Add (b15);

F.Add (b16);

F.Add (b17);

F.Add (b18);

F.Add (b19);

F.PACK ();

F.setVisible (TRUE);

F.Add (F1, "Center");

F.AddWindowListener (this);

F.setsize (300, 150);

F.setVisible (TRUE);

}

// Implement an actionPerformed () method in the ActionListener interface

Public Void ActionPerformed (ActionEvent E) {

String D = E.GetActionCommand ();

CHAR C = D.Charat (0);

Switch (c)

{

Case ' ': // Press the operation symbol for the first time and before it

Case '-': // No number is pressed and not even press twice

Case '*':

Case '/': // first calculate the result

IF (OP! = 0 '&&! Hasop) // Press the equal sign

Result ();

N1 = double.Valueof (Final.getText ()). DoubleValue ();

Op = C;

Hasop = True;

DOT = false;

Break;

Case '=': // Press the equal sign

Result ();

Break;

Case '.': // Press the decimal point

IF (! dot)

{

Final.setText (Final.getText () ".");

Dot = true;

}

Break;

Case 'CE': // Press Clear

N1 = 0;

Op = '0';

Hasop = false;

DOT = false;

Final.Settext ("0");

Break;

DEFAULT: // Press the normal execution of the number

IF (Final.getText (). Equals ("0") || Hasop)

{

Final.Settext (d);

Hasop = false;

}

Else

Final.Settext (Final.getText () D);

}

}

//Calculation results

Private void result ()

{

Double N2 = double.valueof (Final.getText ()). DoubleValue ();

Switch (OP)

{

Case ' ':

ANS = N1 N2;

Break;

Case '-':

ANS = N1 - N2;

Break;

Case '*':

ANS = N1 * 2;

Break;

Case '/':

ANS = N1 / N2;

}

Op = '0';

Hasop = True;

DOT = false;

N1 = ANS;

Final.Settext (String.Valueof (ANS));

}

// Cover the WindowClosing () method in the Windowadapter class

Public void windowClosing (WindowEvent E) {

System.exit (0);

}

转载请注明原文地址:https://www.9cbs.com/read-70256.html

New Post(0)