//2015年11月25日18:21:59
//计算器设计代码1:
import java.awt.*;
import java.awt.event.*;
public class zhang8
{
public static void main(String[] args)
{
new A().f();
}
}
class A
{
String s1 = "";
String s2 = "";
TextField t1;
TextField t2;
TextField t3;
public class B implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1 = Double.valueOf(t1.getText());
double num2 = Double.valueOf(t2.getText());
double num3 = 0;
if (e.getActionCommand().equals("+"))
{
num3 = num1 + num2;
}
if (e.getActionCommand().equals("-"))
{
num3 = num1 - num2;
}
if (e.getActionCommand().equals("*"))
{
num3 = num1 * num2;
}
if (e.getActionCommand().equals("/"))
{
num3 = num1 / num2;
}
String s3 = String.valueOf(num3);
t3.setText(s3);
if(e.getActionCommand().equals("清零"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
public void f()//本函数先设计好界面
{
Frame f = new Frame("张明阳设计的计算器");
f.setVisible(true);
f.setLayout(new GridLayout(3,1));
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER,100,2));
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER,70,2));
Panel p3 = new Panel();
p3.setLayout(new GridLayout(2,3,50,50));
f.add(p1);
f.add(p2);
f.add(p3);
for(int i=0;i<3;++i)
{
Label La = new Label("运算数");
p1.add(La);
}
t1 = new TextField(20);
p2.add(t1);
t2 = new TextField(20);
p2.add(t2);
t3 = new TextField(20);
p2.add(t3);
Button b1 = new Button("+");
p3.add(b1);
Button b2 = new Button("-");
p3.add(b2);
Button b3 = new Button("*");
p3.add(b3);
Button b4 = new Button("/");
p3.add(b4);
Button b5 = new Button("清零");
b5.setBackground(Color.BLUE);
p3.add(b5);
Button b6 = new Button("清零");
b6.setBackground(Color.BLUE);
p3.add(b6);
Font aa = new Font("楷体",Font.BOLD,36);//通过Font类设置文本框字体的格式
b1.setFont(aa);
b2.setFont(aa);
b3.setFont(aa);
b4.setFont(aa);
b5.setFont(aa);
b6.setFont(aa);
b1.addActionListener(new B());//为每一个按钮都安装了事件监听器
b2.addActionListener(new B());
b3.addActionListener(new B());
b4.addActionListener(new B());
b5.addActionListener(new B());
b6.addActionListener(new B());
f.pack();
}
}
//计算器设计代码2
//计算器设计
import java.awt.*;
import java.awt.event.*;
public class zhang1
{
public static void main(String[] args)
{
new A().f();
}
}
class A //本类当中为每一个按钮设置了事件监听器已经更新了作用指令信息
{
public boolean firstFlag = true;
public String s1 = "";
public String s2 = "";
public TextField t ;
public String strOper;
public void f()//本函数用于设计界面
{
Frame f = new Frame("数据分析玩家设计的计算器");
f.setBackground(Color.BLUE);
f.setVisible(true);
f.setLayout(new GridLayout(2,1));
f.addWindowListener(new B());//为关闭按钮设计了一个事件模型
t = new TextField(40);
//t = new TextField("使用之前点击请清零");
Font aa = new Font("楷体",Font.BOLD,36);//通过Font类设置文本框字体的格式
t.setFont(aa);
t.setBackground(Color.white);
Panel p = new Panel();
p.setSize(40,80);
f.add(t);
f.add(p);
p.setLayout(new GridLayout(4,4,20,20));
for(int i=0;i<=9;++i)
{
Button b = new Button(""+i);
b.setActionCommand("数字");
b.addActionListener(new B());//为按钮添加了事件监听器
p.add(b);
}
Button jia = new Button("+");
jia.setActionCommand("算数操作");
jia.addActionListener(new B());//为按钮添加了事件监听器
Button jian = new Button("-");
jian.setActionCommand("算数操作");
jian.addActionListener(new B());//为按钮添加了事件监听器
Button cheng = new Button("*");
cheng.setActionCommand("算数操作");
cheng.addActionListener(new B());//为按钮添加了事件监听器
Button chu = new Button("/");
chu.setActionCommand("算数操作");
chu.addActionListener(new B());//为按钮添加了事件监听器
Button deng = new Button("=");
deng.addActionListener(new B());//为按钮添加了事件监听器
Button qling = new Button("清零");
qling.addActionListener(new B());//为按钮添加了事件监听器
p.add(jia);
p.add(jian);
p.add(cheng);
p.add(chu);
p.add(deng);
p.add(qling);
f.pack();
}
public class B extends WindowAdapter implements ActionListener
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(-1);
}
public void actionPerformed(ActionEvent e)//接下来根据接收到的作用指令信息进行操作
{
String zyIf = e.getActionCommand();//获取发生事件的事件源的作用指令信息
Button bn = (Button)e.getSource();//获取发生事件的事件源按钮
String zfIf = bn.getLabel();
if(zyIf.equals("数字"))//如果事件源是数字键
{
if(firstFlag)
{
s1 = s1 + zfIf;
t.setText(s1);
}
else
{
s2 = s2 + zfIf;
t.setText(s2);
}
}
else if(zyIf.equals("算数操作"))
{
strOper = zfIf;
firstFlag = false;
}
else if(zyIf.equals("="))
{
result();//调用运算函数
}
else if(zyIf.equals("清零"))
{
s1 = s2 = "";
t.setText("");
firstFlag = true;
}
}
public void result()
{
double num1 = Double.valueOf(s1);
double num2 = Double.valueOf(s2);
double num3 = 0;
if (strOper.equals("+"))
{
num3 = num1 + num2;
}
else if (strOper.equals("-"))
{
num3 = num1 - num2;
}
else if (strOper.equals("*"))
{
num3 = num1 * num2;
}
else if (strOper.equals("/"))
{
num3 = num1 / num2;
}
t.setText(""+num3);
s1 = String.valueOf(num3);
s2= "";
}
}
}
//计算器设计代码1:
import java.awt.*;
import java.awt.event.*;
public class zhang8
{
public static void main(String[] args)
{
new A().f();
}
}
class A
{
String s1 = "";
String s2 = "";
TextField t1;
TextField t2;
TextField t3;
public class B implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1 = Double.valueOf(t1.getText());
double num2 = Double.valueOf(t2.getText());
double num3 = 0;
if (e.getActionCommand().equals("+"))
{
num3 = num1 + num2;
}
if (e.getActionCommand().equals("-"))
{
num3 = num1 - num2;
}
if (e.getActionCommand().equals("*"))
{
num3 = num1 * num2;
}
if (e.getActionCommand().equals("/"))
{
num3 = num1 / num2;
}
String s3 = String.valueOf(num3);
t3.setText(s3);
if(e.getActionCommand().equals("清零"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
public void f()//本函数先设计好界面
{
Frame f = new Frame("张明阳设计的计算器");
f.setVisible(true);
f.setLayout(new GridLayout(3,1));
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER,100,2));
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER,70,2));
Panel p3 = new Panel();
p3.setLayout(new GridLayout(2,3,50,50));
f.add(p1);
f.add(p2);
f.add(p3);
for(int i=0;i<3;++i)
{
Label La = new Label("运算数");
p1.add(La);
}
t1 = new TextField(20);
p2.add(t1);
t2 = new TextField(20);
p2.add(t2);
t3 = new TextField(20);
p2.add(t3);
Button b1 = new Button("+");
p3.add(b1);
Button b2 = new Button("-");
p3.add(b2);
Button b3 = new Button("*");
p3.add(b3);
Button b4 = new Button("/");
p3.add(b4);
Button b5 = new Button("清零");
b5.setBackground(Color.BLUE);
p3.add(b5);
Button b6 = new Button("清零");
b6.setBackground(Color.BLUE);
p3.add(b6);
Font aa = new Font("楷体",Font.BOLD,36);//通过Font类设置文本框字体的格式
b1.setFont(aa);
b2.setFont(aa);
b3.setFont(aa);
b4.setFont(aa);
b5.setFont(aa);
b6.setFont(aa);
b1.addActionListener(new B());//为每一个按钮都安装了事件监听器
b2.addActionListener(new B());
b3.addActionListener(new B());
b4.addActionListener(new B());
b5.addActionListener(new B());
b6.addActionListener(new B());
f.pack();
}
}
//计算器设计代码2
//计算器设计
import java.awt.*;
import java.awt.event.*;
public class zhang1
{
public static void main(String[] args)
{
new A().f();
}
}
class A //本类当中为每一个按钮设置了事件监听器已经更新了作用指令信息
{
public boolean firstFlag = true;
public String s1 = "";
public String s2 = "";
public TextField t ;
public String strOper;
public void f()//本函数用于设计界面
{
Frame f = new Frame("数据分析玩家设计的计算器");
f.setBackground(Color.BLUE);
f.setVisible(true);
f.setLayout(new GridLayout(2,1));
f.addWindowListener(new B());//为关闭按钮设计了一个事件模型
t = new TextField(40);
//t = new TextField("使用之前点击请清零");
Font aa = new Font("楷体",Font.BOLD,36);//通过Font类设置文本框字体的格式
t.setFont(aa);
t.setBackground(Color.white);
Panel p = new Panel();
p.setSize(40,80);
f.add(t);
f.add(p);
p.setLayout(new GridLayout(4,4,20,20));
for(int i=0;i<=9;++i)
{
Button b = new Button(""+i);
b.setActionCommand("数字");
b.addActionListener(new B());//为按钮添加了事件监听器
p.add(b);
}
Button jia = new Button("+");
jia.setActionCommand("算数操作");
jia.addActionListener(new B());//为按钮添加了事件监听器
Button jian = new Button("-");
jian.setActionCommand("算数操作");
jian.addActionListener(new B());//为按钮添加了事件监听器
Button cheng = new Button("*");
cheng.setActionCommand("算数操作");
cheng.addActionListener(new B());//为按钮添加了事件监听器
Button chu = new Button("/");
chu.setActionCommand("算数操作");
chu.addActionListener(new B());//为按钮添加了事件监听器
Button deng = new Button("=");
deng.addActionListener(new B());//为按钮添加了事件监听器
Button qling = new Button("清零");
qling.addActionListener(new B());//为按钮添加了事件监听器
p.add(jia);
p.add(jian);
p.add(cheng);
p.add(chu);
p.add(deng);
p.add(qling);
f.pack();
}
public class B extends WindowAdapter implements ActionListener
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(-1);
}
public void actionPerformed(ActionEvent e)//接下来根据接收到的作用指令信息进行操作
{
String zyIf = e.getActionCommand();//获取发生事件的事件源的作用指令信息
Button bn = (Button)e.getSource();//获取发生事件的事件源按钮
String zfIf = bn.getLabel();
if(zyIf.equals("数字"))//如果事件源是数字键
{
if(firstFlag)
{
s1 = s1 + zfIf;
t.setText(s1);
}
else
{
s2 = s2 + zfIf;
t.setText(s2);
}
}
else if(zyIf.equals("算数操作"))
{
strOper = zfIf;
firstFlag = false;
}
else if(zyIf.equals("="))
{
result();//调用运算函数
}
else if(zyIf.equals("清零"))
{
s1 = s2 = "";
t.setText("");
firstFlag = true;
}
}
public void result()
{
double num1 = Double.valueOf(s1);
double num2 = Double.valueOf(s2);
double num3 = 0;
if (strOper.equals("+"))
{
num3 = num1 + num2;
}
else if (strOper.equals("-"))
{
num3 = num1 - num2;
}
else if (strOper.equals("*"))
{
num3 = num1 * num2;
}
else if (strOper.equals("/"))
{
num3 = num1 / num2;
}
t.setText(""+num3);
s1 = String.valueOf(num3);
s2= "";
}
}
}