Java Calculator algorithm implementation

Source: Internet
Author: User

Import Java.awt.BorderLayout;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;


Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;




public class Borderjframe extends JFrame {
Private JButton display;
Private JPanel JP;
Private Boolean start;
Private String Lastcommand;
private double result;
Public Borderjframe () {
This.setlayout (New BorderLayout ());
this.result=0;
This.start=true;
This.lastcommand= "=";
This.display=new JButton ("0");
This.display.setEnabled (FALSE);
Add (display, Borderlayout.north);

ActionListener insert=new insertaction ();
ActionListener command=new commandaction ();
int k=7;
This.setsize (300, 200);
Jp=new JPanel ();
Jp.setlayout (New GridLayout (4, 4));
for (int i=1;i<=4;i++) {
for (int j=1;j<=4;j++) {
if (j==4) {
if (i==1) {
This.addbutton ("/", command);
}else if (i==2) {
This.addbutton ("*", command);
}else if (i==3) {
This.addbutton ("-", command);
}
}else if (i==4) {
This.addbutton ("0", insert);
This.addbutton (".", command);
This.addbutton ("=", command);
This.addbutton ("+", command);
Break
}else{
This.addbutton (String.valueof (k), insert);
k++;
}
}
k=k-6;
}
This.add (Jp,borderlayout.center);
}

Private class Commandaction implements actionlistener{


@Override
public void actionperformed (ActionEvent e) {
String Command=e.getactioncommand ();
if (start) {
if (Command.equals ("-")) {
Display.settext (command);
Start=false;
}else{
Lastcommand=command;
}
}else{
Calculate (Double.parsedouble (Display.gettext ()));
Lastcommand=command;
Start=true;
}
}

}

Private class Insertaction implements actionlistener{


@Override
public void actionperformed (ActionEvent e) {
String Input=e.getactioncommand ();
if (start) {
Display.settext ("");
Start=false;
}
Display.settext (Display.gettext () +input);
}
}

private void AddButton (String Label,actionlistener listener) {
JButton button=new JButton (label);
Button.addactionlistener (listener);
This.jp.add (button);
}

public void calculate (double x) {
if (lastcommand.equals ("+")) result+=x;
else if (lastcommand.equals ("-")) result-=x;
else if (lastcommand.equals ("*")) result*=x;
else if (lastcommand.equals ("/")) Result/=x;
else if (lastcommand.equals ("=")) result=x;
This.display.setText ("+result");
}
}

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.