📄 myconmputer.java
字号:
import java.awt.*;
import java.awt.event.*;
public class MyConmputer implements ActionListener,WindowListener
{
private Frame f;
private TextField tf;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
private Panel p;
private String str1="0";
private String str2="0";
private String str3="0";
private String str5="0";
private String str6="0";
private float f1=0;
private float f2=0;
private float f3=0;
private float f5=0;
private float f6=0;
private int count=0;
private Math m;
public MyConmputer()
{
f=new Frame("My Computer");
tf=new TextField(30);
p=new Panel();
b1=new Button("X2");
b2=new Button("√");
b3=new Button("±");
b4=new Button("C");
b5=new Button("7");
b6=new Button("8");
b7=new Button("9");
b8=new Button("+");
b9=new Button("4");
b10=new Button("5");
b11=new Button("6");
b12=new Button("-");
b13=new Button("1");
b14=new Button("2");
b15=new Button("3");
b16=new Button("*");
b17=new Button("0");
b18=new Button(".");
b19=new Button("=");
b20=new Button("/");
}
public void go()
{
f.add(tf,"North");
p.setLayout(new GridLayout(5,4));
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p.add(b17);
p.add(b18);
p.add(b19);
p.add(b20);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
b20.addActionListener(this);
f.addWindowListener(this);
f.setSize(200, 200);
f.add(p,"Center");
//f.pack();
f.setVisible(true);
tf.setText(str1);
}
public static void main(String args[])
{
MyConmputer c=new MyConmputer();
c.go();
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void actionPerformed(ActionEvent e)
{
if((str1=="0"||str5!="0")&&e.getActionCommand()!="±")//||str5!="0"
{str1=e.getActionCommand();
tf.setText(str1);
str5="0";
}
else if(e.getActionCommand()!="+"&&e.getActionCommand()!="-"&&e.getActionCommand()!="*"&&e.getActionCommand()!="/"&&e.getActionCommand()!="="&&e.getActionCommand()!="±"&&e.getActionCommand()!="X2"&&e.getActionCommand()!="√")
{
str1=str1+e.getActionCommand();
tf.setText(str1);
str2=str2+e.getActionCommand();
//String str=e.getActionCommand();
}
else if(e.getActionCommand()=="+"||e.getActionCommand()=="-"||e.getActionCommand()=="*"||e.getActionCommand()=="/")
{
f1=Float.valueOf(str1).floatValue();
str1=str1+e.getActionCommand();
str2="0";
str3=e.getActionCommand();
tf.setText(str1);
System.out.print("f1="+f1);
}
else if(e.getActionCommand()=="=")
{
f2=Float.valueOf(str2).floatValue();
System.out.print("f2="+f2);
if(str3=="+")
{f3=f1+f2;
str5=Float.toString(f3);
System.out.print("f3="+f3);
tf.setText(str5);
}
if(str3=="-")
{f3=f1-f2;
str5=Float.toString(f3);
System.out.print("f3="+f3);
tf.setText(str5);
}
if(str3=="*")
{f3=f1*f2;
str5=Float.toString(f3);
System.out.print("f3="+f3);
tf.setText(str5);
}
if(str3=="/")
{f3=f1/f2;
str5=Float.toString(f3);
System.out.print("f3="+f3);
tf.setText(str5);
}
}
if(e.getSource()==b3)
{
if(str5!="0")
{ f6=Float.valueOf(str5).floatValue();
f6=f6*(-1);
str5=Float.toString(f6);
tf.setText(str5);
// tf.setText(str5);
System.out.print("str1="+str1+"str5="+str5);
}
else {
f1=Float.valueOf(str1).floatValue();
f1=f1*(-1);
str1=Float.toString(f1);
tf.setText(str1);
}
System.out.print("count="+count);
}
if(e.getSource()==b4)
{ str1="0";
str2="0";
str3="0";
//str5="0";
f1=0;
f2=0;
f3=0;
System.out.print("f1="+f1);
tf.setText(str1);
}
if(e.getSource()==b1)
{
str1="3.1415926535";
tf.setText(str1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -