用Java写一个计算器
1、调用Javax.swing.*(图形化)
import javax.swing.*;
2、初始化它的外形
class WindowOperation extends JFrame {
JMenuBar menuBar; //菜单条
JMenu menu1, menu2, menu3; //菜单
JPanel panel; //按钮面板
JTextArea textArea; //文本区
JButton[][] button = new JButton[6][5]; //30个按钮
String Mem = "0"; //存储区的值
WindowOperation() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
3、设置字体、计算符号等功能
void init() {
setBounds(300, 50, 640, 700); //窗口大小
setTitle("calculator"); //窗口名称
setLayout(new BorderLayout()); //布局样式
LineBorder border = new LineBorder(Color.LIGHT_GRAY, 6, true);//文本区域边框设置
textArea = new JTextArea(2, 10); //计算区域
textArea.setFont(new Font("Calibre", Font.PLAIN, 60)); //设置字体、样式、大小
textArea.setBorder(border);
String [][] str = {
{"Mc", "Mr", "Ms", "M+", "M-"},
{"<—", "CE", "C", "±", "√"},
{"7", "8", "9", "/", "%"},
{"4", "5", "6", "*", "1/x"},
{"1", "2", "3", "-", "^2"},
{"00", "0", ".", "+", "="}
};
panel = new JPanel();
panel.setLayout(new GridLayout(6, 5));
for (int i = 0; i < 6; i++) { //插入各个功能、数字按钮
for (int j = 0; j < 5; j++) {
button[i][j] = new JButton(str[i][j]);
button[i][j].setFont(new Font("黑体", Font.BOLD, 30)); //设置按钮字体、样式、大小
panel.add(button[i][j]);
}
}
add(textArea, BorderLayout.NORTH);
add(panel);
menu1 = new JMenu(" 查看(C) ");
menu1.s