package day23.calc;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyCalc extends JFrame implements ActionListener {
private JMenuBar menu;
private JMenu menuView;
private JMenu menuEdit;
private JMenu menuHelp;
private JLabel lblInput;
private JLabel lblShow;
private JButton[] btnOperater = new JButton[28];
public MyCalc() {
initMenu();
initContent();
this.setTitle("计算器");
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setSize(228, 324);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initMenu() {
menu = new JMenuBar();
menuView = new JMenu("查看(V)");
menuView.setMnemonic('V');//设置热键
menuEdit = new JMenu("编辑(E)");
menuEdit.setMnemonic('E');
menuHelp = new JMenu("帮助(H)");
menuHelp.setMnemonic('H');
menu.add(menuView);
menu.add(menuEdit);
menu.add(menuHelp);
this.setJMenuBar(menu);
}
private void initContent() {
//整个窗口区域
JPanel jp = new JPanel();
jp.setLayout(null);
//显示框区域
JPanel jpShow = new JPanel();
//设置显示框上方小字体
Font myFont = new Font("arial", Font.BOLD, 22);
lblInput = new JLabel();
lblInput.setText("0");
//字体右对齐
lblInput.setHorizontalAlignment(JLabel.RIGHT);
lblInput.setFont(myFont);
//字体
myFont = new Font("arial", Font.BOLD, 10);
lblShow = new JLabel();
lblShow.setText("sqrt");
//设置字体右对齐
lblShow.setHorizontalAlignment(JLabel.RIGHT);
lblShow.setFont(myFont);
//设置位置
jpShow.setBounds(14, 15, 190, 50);
jpShow.setLayout(new BorderLayout());
jpShow.add(lblShow, BorderLayout.NORTH);
jpShow.add(lblInput, BorderLayout.CENTER);
//设置边框
jpShow.setBorder(BorderFactory.createLineBorder(new Color(142, 156, 173)));
jp.add(jpShow);
jpShow = new JPanel();
jpShow.setBounds(14, 72, 196, 194);
jpShow.setLayout(null);
Font font_but = new Font("arial", Font.BOLD, 10);//按钮字体
for (int i = 0; i < btnOperater.length; i++) {
btnOperater[i] = new JButton(Operator.labs[i]);
btnOperater[i].setForeground(new Color(30, 57, 91));
btnOperater[i].setActionCommand(Operator.values()[i].toString());
btnOperater[i].setFont(font_but);//设置按钮字体
btnOperater[i].setBounds(i % 5 * (33 + 6), i / 5 * (24 + 6), 33, 24);//33宽,24高,间距6,5个换一行
btnOperater[i].setBorder(new LineBorder(new Color(135, 151, 170), 1));//按钮边框
btnOperater[i].addActionListener(this);//添加
jpShow.add(btnOperater[i]);
}
btnOperater[24].setBounds(24 % 5 * (33 + 6), 24 / 5 * (24 + 6), 33, 24 * 2 + 6);//调整等号按钮位置
btnOperater[25].setBounds(25 % 5 * (33 + 6), 25 / 5 * (24 + 6), 33 * 2 + 6, 24);//调整0按钮的位置
btnOperater[26].setBounds(27 % 5 * (33 + 6), 26 / 5 * (24 + 6), 33, 24);//调整.按钮的位置
btnOperater[27].setBounds(28 % 5 * (33 + 6), 27 / 5 * (24 + 6), 33, 24);//调整较好按钮的位置
jp.add(jpShow);
this.setLayout(new BorderLayout());
this.add(jp, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args) {
new MyCalc();
}
@Override
public void actionPerformed(ActionEvent e) {
//实现逻辑操作
JButton btn = (JButton) e.getSource();
String commond = btn.getActionCommand();
}
}
//枚举类
enum Operator {
mC, mR, mS, mAdd, mSubtract,//mc,mr,ms,m+,m-
clBack, clError, clClear, operNegation, operSqrt,//<-,ce,c,-/+,开平方
n7, n8, n9, operDivide, operPer,// /,%,
n4, n5, n6, operMultiply, operReciproc,//*,倒数
n1, n2, n3, operSubtract, operEqu,//-,=
n0, operDot, operAdd;//+
public static final String[] labs = new String[]{
"MC", "MR", "MS", "M+", "M-",
"←", "CE", "C", "±", "√",
"7", "8", "9", "/", "%",
"4", "5", "6", "*", "1/x",
"1", "2", "3", "-", "=",
"0", ".", "+"
};
}