📄 jsq.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JSQ extends Frame implements ActionListener
{
double sum=0.0;
double sum1=0.0;
char fuhao;
String str;
boolean panduan,dian;
boolean ab1=false;
MenuBar bar=new MenuBar();
Menu a=new Menu("编辑");
Menu b=new Menu("查看");
Menu d=new Menu("帮助");
MenuItem item1=new MenuItem("复制");
MenuItem item2=new MenuItem("粘贴");
MenuItem item3=new MenuItem("标准型");
MenuItem item4=new MenuItem("科技型");
MenuItem item5=new MenuItem("数字分组");
MenuItem item6=new MenuItem("帮助主题");
MenuItem item7=new MenuItem("关于计算器");
Frame f=new Frame("计算器");
Label WB;
Label a0;
Button c1=new Button("1");
Button c2=new Button("2");
Button c3=new Button("3");
Button c4=new Button("4");
Button c5=new Button("5");
Button c6=new Button("6");
Button c7=new Button("7");
Button c8=new Button("8");
Button c9=new Button("9");
Button c10=new Button("/");
Button c11=new Button("*");
Button c12=new Button("-");
Button c13=new Button("+");
Button c14=new Button(".");
Button c15=new Button("0");
Button c16=new Button("+/-");
Button c17=new Button("sqrt");
Button c18=new Button("%");
Button c19=new Button("/x");
Button c20=new Button("=");
Button b1=new Button("MC");
Button b2=new Button("MR");
Button b3=new Button("MS");
Button b4=new Button("M+");
/* TextField a0=new TextField(""); */
Button a1=new Button("Backspace");
Button a2=new Button(" CE ");
Button a3=new Button(" C ");
Panel p0=new Panel();
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
JSQ()
{
str="";
panduan=dian=false;
a0=new Label();
a0.setForeground(Color.white);
a0.setBackground(Color.white);
WB=new Label("0.");
WB.setAlignment(2);
WB.setForeground(Color.red);
WB.setBackground(Color.white);
bar.add(a);
bar.add(b);
bar.add(d);
a.add(item1);
a.add(item2);
b.add(item3);
b.add(item4);
b.addSeparator();
b.add(item5);
d.add(item6);
d.addSeparator();
d.add(item7);
f.setMenuBar(bar);
p1.setLayout(new BorderLayout(10,10));
p1.add(WB,BorderLayout.CENTER);
p2.setLayout(new BorderLayout(10,10));
p4.setLayout(new FlowLayout(FlowLayout.LEFT,9,5));
p4.add(a0);
p4.add(a1);
p4.add(a2);
p4.add(a3);
p2.add(p4,BorderLayout.NORTH);
p3.setLayout(new GridLayout(4,1,10,10));
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p2.add(p3,BorderLayout.WEST);
p5.setLayout(new GridLayout(4,5,10,10));
p5.add(c7);
p5.add(c8);
p5.add(c9);
p5.add(c10);
p5.add(c17);
p5.add(c4);
p5.add(c5);
p5.add(c6);
p5.add(c11);
p5.add(c18);
p5.add(c1);
p5.add(c2);
p5.add(c3);
p5.add(c12);
p5.add(c19);
p5.add(c15);
p5.add(c16);
p5.add(c14);
p5.add(c13);
p5.add(c20);
p2.add(p5,BorderLayout.CENTER);
p0.setLayout(new BorderLayout());
p0.add(p1,BorderLayout.NORTH);
p0.add(p2,BorderLayout.CENTER);
f.setLayout(new GridLayout());
f.add(p0);
c7.setForeground(Color.blue);
c8.setForeground(Color.blue);
c9.setForeground(Color.blue);
c4.setForeground(Color.blue);
c5.setForeground(Color.blue);
c6.setForeground(Color.blue);
c1.setForeground(Color.blue);
c2.setForeground(Color.blue);
c3.setForeground(Color.blue);
c16.setForeground(Color.blue);
c14.setForeground(Color.blue);
c17.setForeground(Color.blue);
c18.setForeground(Color.blue);
c19.setForeground(Color.blue);
c15.setForeground(Color.blue);
a1.setForeground(Color.red);
a2.setForeground(Color.red);
a3.setForeground(Color.red);
b1.setForeground(Color.red);
b2.setForeground(Color.red);
b3.setForeground(Color.red);
b4.setForeground(Color.red);
c10.setForeground(Color.red);
c11.setForeground(Color.red);
c12.setForeground(Color.red);
c13.setForeground(Color.red);
c20.setForeground(Color.red);
c20.setBackground(Color.white);
c19.setBackground(Color.white);
c18.setBackground(Color.white);
c17.setBackground(Color.white);
c16.setBackground(Color.white);
c15.setBackground(Color.white);
c14.setBackground(Color.white);
c13.setBackground(Color.white);
c12.setBackground(Color.white);
c11.setBackground(Color.white);
c10.setBackground(Color.white);
c9.setBackground(Color.white);
c8.setBackground(Color.white);
c7.setBackground(Color.white);
c6.setBackground(Color.white);
c5.setBackground(Color.white);
c4.setBackground(Color.white);
c3.setBackground(Color.white);
c2.setBackground(Color.white);
c1.setBackground(Color.white);
b4.setBackground(Color.white);
b3.setBackground(Color.white);
b2.setBackground(Color.white);
b1.setBackground(Color.white);
a3.setBackground(Color.white);
a2.setBackground(Color.white);
a1.setBackground(Color.white);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
c5.addActionListener(this);
c6.addActionListener(this);
c7.addActionListener(this);
c8.addActionListener(this);
c9.addActionListener(this);
c10.addActionListener(this);
c11.addActionListener(this);
c12.addActionListener(this);
c13.addActionListener(this);
c14.addActionListener(this);
c15.addActionListener(this);
c16.addActionListener(this);
c17.addActionListener(this);
c18.addActionListener(this);
c19.addActionListener(this);
c20.addActionListener(this);
f.setBackground(Color.lightGray);
f.setResizable(false);
f.setSize(250,250);
double w=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double h=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
f.setLocation((int)((w-250)/2),(int)((h-250)/2));
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
char y=e.getActionCommand().charAt(0);
if(e.getSource()==c19)
if(sum!=0){
sum=1/sum;
WB.setText(String.valueOf(sum));}
else
WB.setText("除数不能为零");
if (e.getSource()==a3)
{
str="";
String abc="0.";
sum=sum1=0;
panduan=dian=false;
WB.setText(abc);
}
else if(e.getSource()==a1)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==a2)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==c18)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==c17)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==b1)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==b2)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==b3)
WB.setText("该功能暂时无法实现!!");
else if(e.getSource()==b4)
WB.setText("该功能暂时无法实现!!");
else{
switch(y)
{
case '+':
if (ab1==false){
panduan=true;
fuhao='+';
str="";
dian=false;
break;
}
else
break;
case '-':
if (ab1==false){
panduan=true;
fuhao='-';
str="";
dian=false;
break;
}
else
break;
case '*':
if (ab1==false){
panduan=true;
fuhao='*';
str="";
dian=false;
break;
}
else
break;
case '/':
if (ab1==false){
panduan=true;
fuhao='/';
str="";
dian=false;
break;
}
else
break;
case '=':
switch(fuhao)
{
case '+':
panduan=dian=false;
sum+=sum1;
WB.setText(String.valueOf(sum));
ab1=false;
break;
case '-':
panduan=dian=false;
sum-=sum1;
WB.setText(String.valueOf(sum));
ab1=false;
break;
case '*':
sum*=sum1;
panduan=dian=false;
WB.setText(String.valueOf(sum));
ab1=false;
break;
case '/':
if(sum1!=0){
sum=sum/sum1;
panduan=true;
fuhao='/';
WB.setText(String.valueOf(sum));
ab1=false;
break;
}
else {
ab1=false;
WB.setText("除数不能为零");
break;}
}
break;
case '.':
if(dian==true)
break;
else{
str=str+String.valueOf(".");
WB.setText(str);
dian=true;
}
break;
default:
str=str+String.valueOf(y);//将用户点的数和以前点数的都连接起来
WB.setText(str);//显示在标签中
if(panduan==false)//如果还没有点运算符的话,说明用户还在输入第一个操作数
sum=Double.parseDouble(str);//那么就将这个数保存在sum中
else//否则就是输入的第二的操作数
sum1=Double.parseDouble(str);//保存在sum1中
break;
}
}
}
public static void main(String args[])
{
JSQ my=new JSQ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -