A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 337091921 于 2013-5-11 21:45 编辑

要实现基本功能 和界面

1 个回复

倒序浏览
//突然感觉把难点克服后写起来巨简单   好多都是重复性工作   本来还可以完善的 不过算了 没那个精力了
import java.awt.*;
import java.awt.event.*;

public class TextCounter
{
    public static void main(String[] args)
    {
    Frame f = new CFrame("MY Frame");
    }

}

// ************************ 窗口及布局***** *******************
class CFrame extends Frame
{

    TextField t;
    Panel p;
    Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bsum, bjian, bcheng, bchu,
        bc,bd;

    CFrame(String s)
    {
    super(s);
    setLocation(300, 100);
    setSize(240, 320);
    setLayout(new BorderLayout());// 容器布局
    t = new TextField();
    add(t, BorderLayout.NORTH);// 添加显示框
    p = new Panel();// 创建面板
    p.setLayout(new GridLayout(4, 5));
    b0 = new Button("0");
    b1 = new Button("1");
    b2 = new Button("2");
    b3 = new Button("3");
    b4 = new Button("4");
    b5 = new Button("5");
    b6 = new Button("6");
    b7 = new Button("7");
    b8 = new Button("8");
    b9 = new Button("9");
    bsum = new Button("+");
    bjian = new Button("-");
    bcheng = new Button("*");
    bchu = new Button("/");
    bc = new Button("c");
    bd = new Button("=");
    p.add(b0);
    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(bsum);
    p.add(bjian);
    p.add(bcheng);
    p.add(bchu);
    p.add(bc);
    p.add(bd);
    // 注册监听 和添加在一起
    ButtonMouitor bh = new ButtonMouitor(this);
    b0.addActionListener(bh);
    b1.addActionListener(bh);
    b2.addActionListener(bh);
    b3.addActionListener(bh);
    b4.addActionListener(bh);
    b5.addActionListener(bh);
    b6.addActionListener(bh);
    b7.addActionListener(bh);
    b8.addActionListener(bh);
    b9.addActionListener(bh);
    bsum.addActionListener(bh);
    bjian.addActionListener(bh);
    bcheng.addActionListener(bh);
    bchu.addActionListener(bh);
    bc.addActionListener(bh);
    bd.addActionListener(bh);

    add(p);// 把面板p添加到容器中去
    this.addWindowListener(new MyWindowClos());// 关闭窗口  的监听注册
    setBackground(Color.blue);
    setTitle("计算器--六道作品");
    setVisible(true);
    }

    // 关闭窗口
    class MyWindowClos extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
        setVisible(false);
        System.exit(0);
    }
    }

}

// ************************ 监听按钮事件处理************************
class ButtonMouitor implements ActionListener
{
    private String s1 = "", s2 = "", s11 = "", s22 = "";
    private int i = 0;// 用来判断 1 加法, 2 减法, 3 乘法, 4 除法
    // 持有对方引用
    CFrame cf = null;

    public ButtonMouitor(CFrame cf)
    {
    this.cf = cf;

    }

    public void actionPerformed(ActionEvent e)
    {
    System.out.println("ok");

    if (e.getSource() == cf.b0)
    {
        s1 += "0";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b1)
    {
        s1 += "1";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b2)
    {
        s1 += "2";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b3)
    {
        s1 += "3";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b4)
    {
        s1 += "4";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b5)
    {
        s1 += "5";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b6)
    {
        s1 += "6";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b7)
    {
        s1 += "7";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b8)
    {
        s1 += "8";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.b9)
    {
        s1 += "9";
        cf.t.setText(s1);
    }
    if (e.getSource() == cf.bsum)// 加号处理
    {
        s11 += s1;
        s1 = "";
        cf.t.setText("");
        i = 1;// 用来判断是什么号
    }
    if (e.getSource() == cf.bjian)// 减号处理
    {
        s11 =  s1;
        s1 = "";
        cf.t.setText("");
        i = 2;
    }
    if (e.getSource() == cf.bcheng)// 乘号处理
    {
        s11 = s1;
        s1 = "";
        cf.t.setText("");
        i = 3;
    }
    if (e.getSource() == cf.bchu)// 除号处理
    {
        s11 = s1;
        s1 = "";
        cf.t.setText("");
        i = 4;
    }
    if(e.getSource()==cf.bc)//清空处理
        {
        cf.t.setText("");
        s1 = "";
        s11 = "";
        }
    // **************************等号处理
    if (e.getSource() == cf.bd)
    {
        if(i==1)
        {
        int n1 = Integer.parseInt(s11);
        int n2 = Integer.parseInt(cf.t.getText());
        System.out.println(n1);
        System.out.println(n2);
        cf.t.setText(""+(n1+n2));
        s1 = "";
        s11 = "";
        }
        else if(i==2)
        {
        int n1 = Integer.parseInt(s11);
        int n2 = Integer.parseInt(cf.t.getText());
        cf.t.setText(""+(n1-n2));
        s1 = "";
        s11 = "";
        
        }
        else if(i==3)
        {
        int n1 = Integer.parseInt(s11);
        int n2 = Integer.parseInt(cf.t.getText());
        cf.t.setText(""+(n1*n2));
        s1 = "";
        s11 = "";
        }
        else if(i==4)
        {
        int n1 = Integer.parseInt(s11);
        int n2 = Integer.parseInt(cf.t.getText());
        cf.t.setText(""+(n1/n2));
        s1 = "";
        s11 = "";
        }
        
        
    }

    }

}
//自己写的  你看看把
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马