zoukankan      html  css  js  c++  java
  • 利用绝对布局制作两个数加减乘除的简易计算器

    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;


    public class ZC extends JFrame
    {
    public ZC()
    {
    setTitle("简易计算器");
    setLayout(null);
    setBounds(0,0,400,400);
    @SuppressWarnings("unused")
    Container c=getContentPane();//建立容器
    JLabel a1=new JLabel("第一个数");//建立标签
    JLabel a2=new JLabel("第二个数");
    JLabel a3=new JLabel("结果");
    JButton b1=new JButton("+");//建立按钮
    JButton b2=new JButton("-");
    JButton b3=new JButton("*");
    JButton b4=new JButton("/");
    b1.setBounds(70,220,50,30);//设置按钮大小
    b2.setBounds(140,220,50,30);
    b3.setBounds(210,220,50,30);
    b4.setBounds(280,220,50,30);
    a1.setBounds(10,30,100,30);
    a2.setBounds(10,80,100,30);
    a3.setBounds(10,130,100,30);
    JTextField t1=new JTextField("请输入第一个数");
    JTextField t2=new JTextField("请输入第二个数");
    JTextField t3=new JTextField("结果");
    t1.setBounds(100,30,150,30);
    t2.setBounds(100,80,150,30);
    t3.setBounds(100,130,150,30);
    c.add(a1);
    c.add(a2);
    c.add(a3);
    c.add(t1);
    c.add(t2);
    c.add(t3);
    c.add(b1);
    c.add(b2);
    c.add(b3);
    c.add(b4);
    class jia implements ActionListener//加法
    {
    public void actionPerformed(ActionEvent c)
    {
    int a =Integer.parseInt(t1.getText());//获取t1输入的内容
    int b =Integer.parseInt(t2.getText());//获取t2输入的内容
    String a1=Integer.toString(a+b);
    t3.setText(a1);//在t3中输出结果
    }
    }
    class jian implements ActionListener//减法
    {
    public void actionPerformed(ActionEvent c)
    {
    int a =Integer.parseInt(t1.getText());
    int b =Integer.parseInt(t2.getText());
    String a1=Integer.toString(a-b);
    t3.setText(a1);
    }
    }
    class cheng implements ActionListener//乘
    {
    public void actionPerformed(ActionEvent c)
    {
    int a =Integer.parseInt(t1.getText());
    int b =Integer.parseInt(t2.getText());
    String a1=Integer.toString(a*b);
    t3.setText(a1);
    }
    }
    class chu implements ActionListener//除
    {
    public void actionPerformed(ActionEvent c)
    {
    int a =Integer.parseInt(t1.getText());
    int b =Integer.parseInt(t2.getText());
    double a1=a;
    double b1=b;
    String z=Double.toString(a1/b1);
    t3.setText(z);
    }
    }
    b1.addActionListener(new jia());
    b2.addActionListener(new jian());
    b3.addActionListener(new cheng());
    b4.addActionListener(new chu());
    setVisible(true);


    }


    public static void main(String[] args)
    {
    new ZC();

    }

    }

  • 相关阅读:
    linux的vim按了ctrl+s之后假死的解决办法
    linux下的终端模拟器urxvt的配置
    vim下正则表达式的非贪婪匹配
    linux中的一个看图的软件
    解决windows的控制台显示utf8乱码的问题
    [PHP][位转换积累]之异或运算的简单加密应用
    [PHP][REDIS]phpredis 'RedisException' with message 'read error on connection'
    [PHP][位转换积累]之与运算截取二进制流的值
    [PHP][位转换积累]之pack和unpack
    [正则表达式]PCRE反向分组引用
  • 原文地址:https://www.cnblogs.com/love-xi/p/4857801.html
Copyright © 2011-2022 走看看