• 简易计算器


    需求分析:创建一个窗体用局部管理器来进行管理,

    一次进行递加然后把局部管理器改成流水布局让它给TextField传递参数,

    再用处理器来提供用户输入的信息对其进行计算。

     package oo;

    import java.awt.Choice;
    import java.awt.Button;
    import java.awt.Frame;
    import java.awt.Label;
    import java.awt.FlowLayout;
    import java.awt.TextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class Text
    {
    /*
    * static TextField tf1; static TextField tf2; static Choice choice; static
    * TextField tf3;
    public static void main(String[] args)

    {
    Frame frame = new Frame("我的第一个窗体");
    frame.setSize(200,100);
    frame.setLocation(200,150);
    frame.addWindowListener(new Listener1());
    final TextField hm1 = new TextField(10);
    final Choice choice = new Choice(); //定义一个Choice
    choice.addItem("+");
    choice.addItem("-");
    choice.addItem("*");
    choice.addItem("/");
    final TextField hm2 = new TextField(8);
    Label label = new Label("=");
    final TextField hm3 = new TextField(8);
    Button button = new Button("计算");
    frame.add(hm1);
    frame.add(choice);
    frame.add(hm2);
    frame.add(label);
    frame.add(hm3);
    frame.add(button);
    frame.setLayout(new FlowLayout());
    button.addActionListener(new ActionListener()
    @Override

    public void actionPerformed(ActionEvent arg0)
    {
    String p1 = hm1.getText();
    String p2 = hm2.getText();
    String ch = choice.getSelectedItem();
    double w1 = Double.parseDouble(p1); //用封装类的parseDouble把字符串转换成 Double
    double w2 = Double.parseDouble(p2);
    double w = 0;
    if (ch.equals("+"))
    {
    w =w1 +w2;
    }
    else if (ch.equals("-"))
    {
    w =w1 - w2;
    }
    else if (ch.equals("*"))
    {
    w = w1 * w2;
    }
    else
    {
    w =w1 /w2;
    }
    hm3.setText(w + "");
    }
    });
    frame.setVisible(true);
    }

    }

    总结:代码对于我来说有点难,这是通过我们Java老师所讲的对于不太懂得地方问同学写的。

    我以后会不断地学习把我不懂的通过查资料问老师和同学,从而来慢慢提高对代码的驾驭能力。

  • 相关阅读:
    FFT 和 NTT
    神秘构造题
    P4396 [AHOI2013]作业
    杜教筛学习笔记
    杜教筛
    「$mathcal{Darkbzoj}$」神犇和蒟蒻
    「CQOI2015」选数
    「$mathcal{Atcoder}$」$mathcal{ARC101}$
    「NOI2019」退役记???
    「李超线段树」
  • 原文地址:https://www.cnblogs.com/MM2659463510/p/4859619.html
Copyright © 2020-2023  润新知