zoukankan      html  css  js  c++  java
  • 简单界面生成

    import java.awt.*; impo

    rt java.awt.event.*;

    class Calculate extends Frame implements ActionListener {

    TextField t1=new TextField(5); //第一个操作数文本框

    TextField t2=new TextField(5); //运算符文本框

    TextField t3=new TextField(5); //第一个操作数文本框

    TextField t4=new TextField(5); //结果文本框

    Label L1=new Label("=");

    Button btn=new Button("计算");

    public Calculate() {

    setLayout(new FlowLayout());

    add(t1);add(t2);

    add(t3);

    add(L1);

    add(t4);

    add(btn);

    btn.addActionListener(this); //注册动作事件监听者为当前对象

    addWindowListener(new WindowAdapter() {

    //关闭窗口事件

    public void windowClosing(WindowEvent e)

    { dispose(); //释放窗口 System.exit(0); //退出程序 }

    });

    }

    public void actionPerformed(ActionEvent e)

    {

    float x,y; //操作数变量

    double result=0; //结果变量

    String op;

    try {//异常捕获机制

    x=Float.parseFloat(t1.getText());//将字符串数据转换成浮点型数据

    y=Float.parseFloat(t3.getText());

    op=t2.getText();

    if(op.equals("+")) //运算符为"+" result=x+y;

    else if(op.equals("-")) //运算符为"-" result=x-y;

    else if(op.equals("*")) //运算符为"*" result=x*y;

    else if(op.equals("/")) //运算符为"/" result=x/y;

    t4.setText(Double.toString(result));

    }

    catch(Exception ee){t4.setText("数据错误");

    } //捕获异常,数据错误时,显示信息

    }

    public static void main(String args[]) {

    Calculate mainFrame = new Calculate();

    mainFrame.setSize(400, 400);

    mainFrame.setTitle("两个数的计算程序");

    mainFrame.setVisible(true);

    }

    }

  • 相关阅读:
    20.12.21 leetcode316
    20.12.18 leetcode389
    NOIP2017退役记
    DNA序列 LOJ NOIP模拟赛 D1T1 字符串哈希
    解药还是毒药 codevs2594 状态压缩 BFS
    换教室 vijos2005 NOIP2016 D1T3 期望DP 图论最短路(雾)
    都市大飙车 UESTC 1652 概率DP
    添加括号 vijos1038 动态规划 区间DP
    一道神奇的并查集
    还有一道神奇的暴力(正解是要旋转坐标轴的)
  • 原文地址:https://www.cnblogs.com/xiaokuan/p/5469949.html
Copyright © 2011-2022 走看看