Software Engineering Second Job

Source: Internet
Author: User

The code runs as follows:

Mport javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;

public class Heya1 extends JFrame implements ActionListener
{
Private Boolean dotexist, operated, equaled; Boolean variable for help operation
Private double storednumber; The current results
Private char lastoperator; Represents the previous operator
Private JTextField operation; Result Bar
Private JButton dot, plus, minus, multi, div, sqrt, equal, CHANGEPN, clear; Operator
Private jbutton[] numbers; Digital

Construction person
Public Heya1 ()
{
Settitle ("Calculator");
Initialize variables
Dotexist = false; Indicates whether the current number has a decimal point
operated = FALSE; Indicates whether any operator is pressed
equaled = false; Indicates whether the equals sign is pressed
Storednumber = 0;
Lastoperator = '? ';
Initializing window variables
Operation = new JTextField ("0");
Operation.seteditable (FALSE);
Numbers = new JBUTTON[10];
for (int i = 0; i <; i++)
Numbers[i] = new JButton ("" + i);
dot = new JButton (".");
plus = new JButton ("+");
minus = new JButton ("-");
Multi = new JButton ("*");
div = new JButton ("/");
sqrt = new JButton ("√");
equal = new JButton ("=");
CHANGEPN = new JButton ("±");
Clear = new JButton ("AC");
Put the Window object into the window
GridBagLayout layout = new GridBagLayout ();
Getcontentpane (). setlayout (layout);
AddComponent (layout, operation, 0, 0, 4, 1);
AddComponent (layout, numbers[1], 1, 0, 1, 1);
AddComponent (layout, numbers[2], 1, 1, 1, 1);
AddComponent (layout, numbers[3], 1, 2, 1, 1);
AddComponent (layout, numbers[4], 2, 0, 1, 1);
AddComponent (layout, numbers[5], 2, 1, 1, 1);
AddComponent (layout, numbers[6], 2, 2, 1, 1);
AddComponent (layout, numbers[7], 3, 0, 1, 1);
AddComponent (layout, numbers[8], 3, 1, 1, 1);
AddComponent (layout, numbers[9], 3, 2, 1, 1);
AddComponent (Layout, dot, 4, 0, 1, 1);
AddComponent (layout, numbers[0], 4, 1, 1, 1);
AddComponent (Layout, sqrt, 4, 2, 1, 1);
AddComponent (layout, plus, 1, 3, 1, 1);
AddComponent (layout, minus, 2, 3, 1, 1);
AddComponent (layout, multi, 3, 3, 1, 1);
AddComponent (Layout, Div, 4, 3, 1, 1);
AddComponent (Layout, equal, 5, 0, 2, 1);
AddComponent (Layout, CHANGEPN, 5, 2, 1, 1);
AddComponent (Layout, clear, 5, 3, 1, 1);
}

How to react to a button
public void actionperformed (ActionEvent e)
{
JButton btn = (JButton) e.getsource ();
if (btn = = clear)
{
Operation.settext ("0");
Dotexist = false;
Storednumber = 0;
Lastoperator = '? ';
}
else if (btn = = equal)
{
Operate (' = ');
equaled = true;
}
else if (btn = = Plus)
{
Operate (' + ');
equaled = false;
}
else if (btn = = minus)
{
Operate ('-');
equaled = false;
}
else if (btn = = multi)
{
Operate (' * ');
equaled = false;
}
else if (btn = = div)
{
Operate ('/');
equaled = false;
}
else if (btn = = CHANGEPN)
{
Operate (' P ');
Operate (' = ');
equaled = true;
}
else if (btn = = sqrt)
{
Operate (' s ');
Operate (' = ');
equaled = true;
}
Else
{
if (equaled)
Storednumber = 0;
for (int i = 0; i <; i++)
if (btn = = Numbers[i])
{
if (Operation.gettext (). Equals ("0"))
Operation.settext ("" + i);
else if (! Operated)
Operation.settext (Operation.gettext () + i);
Else
{
Operation.settext ("" + i);
operated = FALSE;
}
}
if (btn = = Dot &&! dotexist)
{
Operation.settext (Operation.gettext () + ".");
Dotexist = true;
}
}
}
How to perform the operation
private void Operate (char operator)
{
Double currentnumber = double.valueof (Operation.gettext ()). Doublevalue ();
if (Lastoperator = = '? ')
Storednumber = Currentnumber;
else if (Lastoperator = = ' + ')
Storednumber + = Currentnumber;
else if (lastoperator = = '-')
Storednumber-= Currentnumber;
else if (lastoperator = = ' * ')
Storednumber *= Currentnumber;
else if (lastoperator = = '/')
Storednumber/= Currentnumber;
else if (lastoperator = = ' P ')
Storednumber *=-1;
else if (Lastoperator = = ' s ')
Storednumber = Math.sqrt (Currentnumber);
else if (lastoperator = = ' = ' && equaled)
Storednumber = Currentnumber;
Operation.settext ("" + storednumber);
operated = TRUE;
Lastoperator = operator;
}

A quick way to use GridBagLayout
private void AddComponent (GridBagLayout layout, Component Component, int row, int col, int width, int height)
{
gridbagconstraints constraints = new gridbagconstraints ();
Constraints.fill = Gridbagconstraints.both;
Constraints.insets = new Insets (10, 2, 10, 2);
CONSTRAINTS.WEIGHTX = 100;
constraints.weighty = 100;
Constraints.gridx = col;
Constraints.gridy = row;
Constraints.gridwidth = width;
Constraints.gridheight = height;
Layout.setconstraints (component, constraints);
If (component instanceof JButton)
((JButton) component). addActionListener (this);
Getcontentpane (). Add (component);
}

The Main method initializes and displays the window
public static void Main (string[] args)
{
HEYA1 calc = new Heya1 ();
Calc.setsize (290, 400);
Calc.setvisible (TRUE);
}
}

The addition runs as follows:

The code for 1:4+5 is as follows

Subtraction runs as follows

2:5-4 of the running results

:

3: Multiplication Run

Results of 4*5 operation

4: Division of the Run

Results of 5/4

Software Engineering Second Job

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.