嘿嘿 ,我今天写了一个代码,给大家看看,是计算器 - 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.