📄 counter.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Counter implements ActionListener{
Frame f = new Frame("计算器");
Panel p = new Panel();
TextField tf1 = new TextField(20);
Button[] b = new Button[21];
String[] s = {"退格","CE","C","/",
"7","8","9","*",
"4","5","6","-",
"1","2","3","+",
"0","+/-",".","="};
double d1;
double d2;
double result;
String operator;
boolean flag1;
boolean i = false;
//创建菜单
MenuBar mb = new MenuBar();
Menu m1 = new Menu("Help");
MenuItem mi1 = new MenuItem("about as");
MenuItem mi2 = new MenuItem("quit");
public Counter(){
f.setMenuBar(mb);
mb.add(m1);
mi1.addActionListener(this);
mi2.addActionListener(this);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
tf1.setForeground(Color.black);
tf1.setFont(new Font("Arial Black",1,20));
for(int i=1;i<21;i++){
b[i] = new Button(s[i-1]);
b[i].setFont(new Font("仿宋",1,15));
b[i].addActionListener(this);
}
p.setLayout(new GridLayout(5,4,3,3));
p.setBackground(Color.LIGHT_GRAY);
for(int i=1;i<21;i++){
p.add(b[i]);
b[i].setBackground(new Color(20,130,180));
b[i].setForeground(Color.yellow);
}
for(int i=1;i<4;i++) {
b[i].setBackground(new Color(120,180,170));
b[i].setForeground(Color.blue);
}
for(int i=1;i<=4;i++){
b[i*4].setBackground(new Color(120,180,170));
b[i*4].setForeground(Color.red);
}
b[20].setBackground(Color.red);
f.setLayout(new BorderLayout(3,3));
f.add("North",tf1);
f.add("Center",p);
f.setSize(300,200);
f.setLocation(300,300);
f.setBackground(Color.LIGHT_GRAY);
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
//获得按扭信息
String s = e.getActionCommand();
String s1 = tf1.getText();
if(s=="0"||s=="1"||s=="2"||s=="3"||s=="4"||s=="5"||s=="6"||s=="7"||s=="8"||s=="9"){
if(flag1){
tf1.setText(s);
flag1 = false;
}else{
s1 = s1+s;
tf1.setText(s1);
}
}else if(s =="."){
int j = s1.indexOf(".");
if(j ==-1){
s1 = s1 + s;
tf1.setText(s1);
}
}else if(s=="+"|| s=="-"||s=="*"||s=="/"){
operator = s;
d1 = Double.parseDouble(s1);
flag1 = true;
if(operator=="+"){
//实现连加
if(i){
d1=0;
i = false;
}else{
result += d1;
}
tf1.setText(result+"");
}else if(operator=="-"){
d2 = Double.parseDouble(s1);
result = d1 - d2;
}else if(operator=="*"){
d2 = Double.parseDouble(s1);
result = d1 * d2;
}else{
d2 = Double.parseDouble(s1);
result = d1 / d2;
}
}else if(s=="="){
d2 = Double.parseDouble(s1);
if(operator=="+"){
result += d2;
i = true;
}else if(operator=="-"){
result = d1 - d2;
}else if(operator=="*"){
result = d1 * d2;
}else{
result = d1 / d2;
}
//tf1.setText(result+"");//这样处理会降低效率
tf1.setText(new Double(result).toString());//封装
}else if(s=="退格"){
if(!s1.equals("")){
s1 = s1.substring(0,s1.length()-1);
tf1.setText(s1);
}
}else if(s=="+/-"){
double d=Double.parseDouble(s1);
d = -d;
tf1.setText(String.valueOf(d));//封装另一种写法
}else if(s=="C"){
tf1.setText("");
flag1 = true;
}else if(s=="CE"){
tf1.setText("");
d1 = 0;
d2 = 0;
result = 0;
flag1 = true;
}else if(s=="quit"){
System.exit(0);
}else{
JOptionPane.showMessageDialog(f,"angela 1.0","about",JOptionPane.QUESTION_MESSAGE);//swing
}
}
public static void main(String args[]){
new Counter();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -