黑马程序员_简单的计算器编写(GUI)课堂作业

这篇博客介绍如何用Java编程创建一个简单的图形用户界面(GUI)计算器,涵盖了GUI设计和事件处理基础知识。
摘要由CSDN通过智能技术生成


---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a&gt;、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------


package JiSuanQi;


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import javax.swing.*;

public class JiSuanQi extends JFrame implements ActionListener {
    JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,
            b16;
    JFrame f;
    JPanel p1=new JPanel();
    GridLayout c = new GridLayout(4, 4);
    BorderLayout b= new BorderLayout(0,0);
    JTextField shuru = new JTextField();
    JTextField xianshi = new JTextField();
    String str3="";
    String str1="";
    String str2="";
    String str4="";
    String str5="";
    double value1,value2,resoult=0;
    public JiSuanQi(){
        init();
    }

    public void init() {
        f = new JFrame("计算器");
        Container ct = f.getContentPane();
        shuru.setHorizontalAlignment(JTextField.RIGHT);
        xianshi.setHorizontalAlignment(JTextField.LEFT);
        ct.add(p1,"North");
        p1.setLayout(b);
        p1.add(xianshi, "North");
        p1.add(shuru,"Center");
        JPanel p2 = new JPanel();
        ct.add(p2, "Center");
        GridLayout c = new GridLayout(4, 4,4,4);
        p2.setLayout(c);

        JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16;
        b1 = new JButton("1");
        b2 = new JButton("2");
        b3 = new JButton("3");
        b4 = new JButton("4");
        b5 = new JButton("5");
        b6 = new JButton("6");
        b7 = new JButton("7");
        b8 = new JButton("8");
        b9 = new JButton("9");
        b10 = new JButton("0");
        b11 = new JButton(".");
        b12 = new JButton("=");
        b13 = new JButton("+");
        b14 = new JButton("-");
        b15 = new JButton("*");
        b16 = new JButton("/");
        p2.add(b1);
        b1.addActionListener(this);
        p2.add(b2);
        b2.addActionListener(this);
        p2.add(b3);
        b3.addActionListener(this);
        p2.add(b13);
        b13.addActionListener(this);
        p2.add(b4);
        b4.addActionListener(this);
        p2.add(b5);
        b5.addActionListener(this);
        p2.add(b6);
        b6.addActionListener(this);
        p2.add(b14);
        b14.addActionListener(this);
        p2.add(b7);
        b7.addActionListener(this);
        p2.add(b8);
        b8.addActionListener(this);
        p2.add(b9);
        b9.addActionListener(this);
        p2.add(b15);
        b15.addActionListener(this);
        p2.add(b10);
        b10.addActionListener(this);
        p2.add(b11);
        b11.addActionListener(this);
        p2.add(b12);
        b12.addActionListener(this);
        p2.add(b16);
        b16.addActionListener(this);

    /*    f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });*/
        f.setSize(300, 250);
        f.setVisible(true);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值