java计算器 - GUI

嘿嘿 ,我今天写了一个代码,给大家看看,是计算器 - GUI版的 


/*
 思路 :
 设置三个全局变量, firstNumber,secondNumber,opr
 前两个近视操作数,opr是操作符
 如果按 +  - *  / 等操作符按钮的话,将 opr 赋值给 这些 按钮
   然后给 = 加一个监听 用equals方法判断操作符
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 2 * @Author: Mzx
 * 3 * @Date: 2020/10/24 12:22
 * 4
 */
public class JavaMain implements KeyListener{
    //定义必要的组件
    static JFrame frame;
    static int firstNumber;
    static int secondNumber;
    static String opr;
    static JTextField tx = new JTextField();
    static JButton c = new JButton("C");
    static JButton OK = new JButton("=");
    static JButton zero = new JButton("0");
    static JButton one = new JButton("1");
    static JButton two = new JButton("2");
    static JButton three = new JButton("3");
    static JButton four = new JButton("4");
    static JButton five = new JButton("5");
    static JButton six = new JButton("6");
    static JButton seven = new JButton("7");
    static JButton eight = new JButton("8");
    static JButton nine = new JButton("9");
    static JButton add = new JButton("+");
    static JButton sub = new JButton("-");
    static JButton mul = new JButton("*");
    static JButton div = new JButton("/");

    public static void main(String[] args) {
        frame = new JFrame("计算器 - PC");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  //窗口关闭事件
        frame.setVisible(true);
        frame.setBounds(200, 100, 405, 530);
        frame.setResizable(false);
        frame.setLayout(null);
        tx.setBounds(0, 0, 405, 100);
        tx.setEditable(false);
        tx.setBackground(Color.lightGray);
        Font font = new Font("微软雅黑", Font.BOLD, 30);
        /*因为字体太小了
        所以设置字体
        有三个参数,第二个参数中的BOLD意思是粗体
        */

        frame.add(tx);
        tx.setFont(font);
        c.setFont(font);
        zero.setFont(font);
        one.setFont(font);
        two.setFont(font);
        three.setFont(font);
        four.setFont(font);
        five.setFont(font);
        six.setFont(font);
        seven.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值