📄 calculator2.java
字号:
import java.awt.*;
import java.applet.*;
import java.io.*;
import corejava.*;
public class Calculator2 extends Applet
{ public void init()
{ setLayout(new BorderLayout());
Panel P = new Panel ();
P.setLayout (new GridLayout(2,1));
title=getParameter("title");
Title = new Label(title);
P.add(Title);
display = new TextField("0",35);
display.setEditable(false);
P.add(display);
add("North", P);
Panel p = new Panel();
p.setLayout (new GridLayout(5,4));
p.add(new Button("7"));
p.add(new Button("8"));
p.add(new Button("9"));
p.add(new Button("/"));
p.add(new Button("4"));
p.add(new Button("5"));
p.add(new Button("6"));
p.add(new Button("*"));
p.add(new Button("1"));
p.add(new Button("2"));
p.add(new Button("3"));
p.add(new Button("-"));
p.add(new Button("0"));
p.add(new Button("."));
p.add(new Button("="));
p.add(new Button("+"));
p.add(new Button("Clear"));
add("Center", p);
}
public boolean action(Event evt, Object arg)
{ if (arg instanceof String)
{ String s = (String) arg;
char ch = s.charAt(0);
if (ch == 'C') {resetCalculator(); start=true; return true;}
if ('0' <= ch && ch <= '9' || ch == '.')
{ if (start) display.setText(s);
else display.setText(display.getText() + s);
start = false;
}
else
{ if (start)
{ if (s.equals("-"))
{ display.setText(s); start = false; }
else op = s;
}
else
{ calculate(Format.atof(display.getText()));
op = s;
start = true;
}
}
}
else return super.action(evt, arg);
return true;
}
public void resetCalculator()
{ display.setText ("0");
arg=0;
start=true;
}
public void calculate(double n)
{ if (op == "+") arg += n;
else if (op == "-") arg -= n;
else if (op == "*") arg *= n;
else if (op == "/") arg /= n;
else if (op == "=") arg = n;
display.setText("" + arg);
}
private TextField display;
private Label Title;
private String title;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -