• 利用绝对布局制作两个数加减乘除的简易计算器


    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();

    }

    }

  • 相关阅读:
    设计模式基础:类及类关系的UML表示
    SQL 经典语句
    网络存储
    jstack Dump
    Windows上模拟Linux环境的软件Cygwin
    竞争条件
    Java volatile关键字
    java原子操作
    java死锁小例子
    死锁四个必要条件
  • 原文地址:https://www.cnblogs.com/LJT666/p/5479334.html
Copyright © 2020-2023  润新知