Import the Java. The awt. *;
Import the Java. The awt. Event. *;
The import javax.mail. Swing. *;
Public class computer {
Public static void main (String [] args)
{
The class calculator extends JPanel
{
Private JButton dis.
Private JPanel panel;
Private double the result;
Private String lastCommand;
Private Boolean start;
Public calculator ()
{
Enclosing setLayout (new BorderLayout ());
Result=0;
LastCommand="=";
Start=true;
Dis=new JButton (" 0 ");
Dis. SetEnabled (false);
Dis. Add (dis, BorderLayout. NORTH);
ActionListener insert=new InsertAction ();
ActionListener command=new the CommandAction ();
The panel=new JPanel ();
The panel. SetLayout (new GridLayout (4, 4));
AddButton (" 7 ", insert);
AddButton (" 8 ", insert);
AddButton (" 9 ", insert);
AddButton ("/", the command);
AddButton (" 4 ", insert);
AddButton (" 5 ", insert);
AddButton (" 6 ", insert);
AddButton (" - ", the command);
AddButton (" 1 ", insert);
AddButton (" 2 ", insert);
AddButton (" 3 ", insert);
AddButton (" * ", the command);
AddButton (" 0 ", the insert);
AddButton (". ", insert);
AddButton ("=", the command);
AddButton (" + ", the command);
Add (panel, BorderLayout. CENTER);
}
Public void addButton (String, number, ActionListener listener)
{
JButton Button=new JButton (" number ");
Button. AddActionListener (the listener);
A panel. The add (Button);
}
The class InsertAction implements ActionListener
{
Public void actionPerformed (an ActionEvent event)
{
String Input=event. GetActionCommand ();
If (start)
{
Dis. SetText (" ");
Start=false;
}
Dis. SetText (dis) getText () + Input);
}
}
The class the CommandAction implements ActionListener
{
Public void actionPerformed (an ActionEvent event)
{
String command=event. GetActionCommand ();
If (start) {
If (command) equals (" - ")) {
Dis. SetText (command);
Start=false;
}
The else lastCommand=command;
} else {
Computer (Double parseDouble (dis) getText ()));
LastCommand=command;
Start=true;
}
}
}
Public void computer (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;
Dis. SetText (" "+ result);
}
}
}}
CodePudding user response:
What to try to change Chinese into EnglishCodePudding user response: