程序思路
1.界面设计
首先创建Frame容器,textField文本框,Jpanel面板,
文本框放置在Fram的BorderLayout.North
面板放置在Frame的BorderLayout.Center
创建了成员变量bton
长度为25的bton数组和btonlabel数组
通过for循环创建25个bton对象并设置了相应的label
利用Font设置了bton中label中字体的大小和字体
2.计算功能实现
通过给bton添加监听实现功能
创建了strA,strB和operate
通过一个action变量获得按钮的ActionCommand,
将ActionCommand转化为char类型赋值给act
str+=action利用字符串实现了数字追加
用if判断输入为数字还是运算符,初始化operate为~,通过判断operate是否为~判断用户输入的是第几个数。
当用户按下等号时再用if判断operate为何种运算符进入不同的运算。
在运算中通过继承了Arithmetic类,Arithmetic类接了两个接口,一个实现基础的四则运算,一个实现多功能计算,通过调用方法进行计算,并输出
3. 颜色设置功能
在Frame中添加了menuBar菜单栏,添加了颜色菜单项。
给颜色菜单项添加监听,当被点击时弹出窗口,在窗口里添加了各种颜色的复选框,为了使复选框只能选中一个,将复选框都添加在buttonGroup中。
为各个复选框添加监听,当复选框被选中时设置不同的按钮背景颜色
代码实现
package YMP.practical_Training.Test1_3; public interface base_Operation { double add(int a,int b); double sub(int a,int b); double mul(int a,int b); double div(int a,int b); double add(double a,double b); double sub(double a,double b); double mul(double a,double b); double div(double a,double b); }
package YMP.practical_Training.Test1_3; public interface moreArithmetic { double sin(int a); double cos(int a); double tan(int a); double sin(double a); double cos(double a); double tan(double a); double percent(int a); double percent(double a); double pow(int a,int b); double pow(double a,double b); double sqrt(int a); double sqrt(double a); double f(int a); double minusPower(double a); }
package YMP.practical_Training.Test1_3; public class Arithmetic implements moreArithmetic, base_Operation { @Override public double sin(int a) { return Math.sin(a); } @Override public double cos(int a) { return Math.cos(a); } @Override public double tan(int a) { return Math.tan(a); } @Override public double sin(double a) { return Math.sin(a); } @Override public double cos(double a) { return Math.cos(a); } @Override public double tan(double a) { return Math.tan(a); } @Override public double percent(int a) { return (double)a/100; } @Override public double percent(double a) { return a/100; } @Override public double pow(int a, int b) { return Math.pow(a,b); } @Override public double pow(double a, double b) { return Math.pow(a,b); } @Override public double sqrt(int a) { return Math.sqrt(a); } @Override public double sqrt(double a) { return Math.sqrt(a); } @Override public double f(int a) { if(a==0||a==1){ return 1; }else { return f(a-1)*a; } } @Override public double minusPower(double a) { return pow(a,-1); } @Override public double add(int a, int b) { return a+b; } @Override public double sub(int a, int b) { return a-b; } @Override public double mul(int a, int b) { return a*b; } @Override public double div(int a, int b) { return a/b; } @Override public double add(double a, double b) { return a+b; } @Override public double sub(double a, double b) { return a-b; } @Override public double mul(double a, double b) { return a*b; } @Override public double div(double a, double b) { return a/b; } }
package YMP.practical_Training.Test1_3; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.zip.ZipEntry; public class TestCalculator { public static void main(String[] args) { new MyCalculator().loadMyCalculator(); } } class MyCalculator extends Arithmetic implements ActionListener, MouseListener { String strA = ""; String strB = ""; String strC = ""; char operator = '~'; private JButton[] bton; JTextField textField = new JTextField(10); Font font = new Font("宋体", Font.PLAIN, 30); private JDialog dialog; JCheckBox checkBoxRicegrey = new JCheckBox("米灰"); JCheckBox checkBoxCream = new JCheckBox("米色"); JCheckBox checkBoxCreamGreen = new JCheckBox("奶绿"); JCheckBox checkBoxLemon = new JCheckBox("柠檬"); JCheckBox checkBoxPink = new JCheckBox("佩奇"); JCheckBox checkBoxRed = new JCheckBox("嫣红"); JCheckBox checkBoxWhite=new JCheckBox("雪白"); JCheckBox checkBoxFogBlue=new JCheckBox("雾蓝"); JCheckBox checkBoxSkyBlue=new JCheckBox("天蓝"); ButtonGroup buttonGroup = new ButtonGroup(); Color thisColor; JPanel outerColour = new JPanel(); JPanel inerColour = new JPanel(); String bttonText[] = {"sin", "CE", "X!", "%", "÷", "cos", "7", "8", "9", "×", "tan", "4", "5", "6", "-", "√", "1", "2", "3", "+", "x^y", "1/x", "0", ".", "="}; public void loadMyCalculator() { JFrame frame = new JFrame("计算器"); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(650, 800); frame.setVisible(true); JPanel panel = new JPanel(); bton=new JButton[25]; JMenuBar menuBar = new JMenuBar(); JMenu seting = new JMenu("设置"); JMenuItem colour = new JMenuItem("颜色"); seting.add(colour); menuBar.add(seting); frame.setJMenuBar(menuBar); buttonGroup.add(checkBoxRicegrey); buttonGroup.add(checkBoxCreamGreen); buttonGroup.add(checkBoxCream); buttonGroup.add(checkBoxLemon); buttonGroup.add(checkBoxPink); buttonGroup.add(checkBoxRed); buttonGroup.add(checkBoxWhite); buttonGroup.add(checkBoxFogBlue); buttonGroup.add(checkBoxSkyBlue); dialog = new JDialog(); dialog.setSize(400, 400); dialog.setLayout(new GridLayout(3, 3)); dialog.setLocation(100, 200); dialog.add(checkBoxRicegrey); dialog.add(checkBoxCreamGreen); dialog.add(checkBoxLemon); dialog.add(checkBoxCream); dialog.add(checkBoxPink); dialog.add(checkBoxRed); dialog.add(checkBoxWhite); dialog.add(checkBoxFogBlue); dialog.add(checkBoxSkyBlue); checkBoxCreamGreen.setFont(font); checkBoxPink.setFont(font); checkBoxCream.setFont(font); checkBoxLemon.setFont(font); checkBoxRicegrey.setFont(font); checkBoxRed.setFont(font); checkBoxWhite.setFont(font); checkBoxFogBlue.setFont(font); checkBoxSkyBlue.setFont(font); checkBoxRed.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0xE57677); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxPink.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0xFDE3E3); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxCream.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0xEBE9E0 ); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxLemon.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0xFFF450); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxRicegrey.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0xC2BFAC); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxCreamGreen.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0xF1F5D9 ); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxWhite.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=Color.white; for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxSkyBlue.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0x72D9E2); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); checkBoxFogBlue.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ thisColor=new Color(0x7EBBDD); for(int i=0;i<bton.length;i++){ if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton[i].setBackground(thisColor); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton[i].setBackground(Color.white); } } } } }); colour.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(true); } }); frame.add(panel, BorderLayout.CENTER); panel.setLayout(new GridLayout(5, 5)); textField.setPreferredSize(new Dimension(600, 200)); frame.add(textField, BorderLayout.NORTH); textField.setFont(font); textField.setHorizontalAlignment(SwingConstants.RIGHT); for (int i = 0; i <25; i++) { bton[i]= new JButton(bttonText[i]); bton[i].setFont(font); panel.add(bton[i]); bton[i].addActionListener(this); //设置按钮颜色 /* if (i < 6 || i == 9 || i == 10 || i == 14 || i == 15 || (i >= 19 && i < 25)) { bton.setActionCommand("outer"); } if (i >= 6 && i <= 8 || i >= 11 && i <= 13 || i >= 16 && i <= 18) { bton.setActionCommand("iner"); } */ } bton[0].setActionCommand("s"); bton[5].setActionCommand("c"); bton[10].setActionCommand("t"); bton[15].setActionCommand("√"); bton[20].setActionCommand("^"); bton[1].setActionCommand("E"); bton[1].addMouseListener(this); bton[2].setActionCommand("!"); bton[21].setActionCommand("m"); } @Override public void actionPerformed(ActionEvent e) { String action = e.getActionCommand(); char act = action.charAt(0); if (act >= '0' && act <= '9' || act == '.') { if (operator != '~') { strB += action; if (operator == 's') { textField.setText("sin" + strB); } else if (operator == 'c') { textField.setText("cos" + strB); } else if (operator == 't') { textField.setText("tan" + strB); } else textField.setText(strA + operator + strB); } else { strA += action; textField.setText(strA); } } else if (act == '+' || act == '-' || act == '×' || act == '÷' || act == '%' || act == 's' || act == 'c' || act == 't' || act == '√' || act == '^' || act == '!' || act == 'm') { operator = act; if (operator == 'm') { textField.setText(strA + "(^-1)"); } else if (operator == 's') { textField.setText("sin" + strB); } else if (operator == 'c') { textField.setText("cos" + strB); } else if (operator == 't') { textField.setText("tan" + strB); } else { textField.setText(strA + operator); } } else if (act == '=') { if (operator == '+') { double a = Double.parseDouble(strA); double b = Double.parseDouble(strB); double c = add(a, b); textField.setText(strA + operator + strB + "=" + c); } else if (operator == '-') { double a = Double.parseDouble(strA); double b = Double.parseDouble(strB); double c = sub(a, b); textField.setText(strA + operator + strB + "=" + c); } else if (operator == '×') { double a = Double.parseDouble(strA); double b = Double.parseDouble(strB); double c = mul(a, b); textField.setText(strA + operator + strB + "=" + c); } else if (operator == '÷') { double a = Double.parseDouble(strA); double b = Double.parseDouble(strB); double c = div(a, b); textField.setText(strA + operator + strB + "=" + c); } else if (operator == '%') { double a = Double.parseDouble(strA); double c = percent(a); textField.setText(strA + operator + "=" + c); } else if (operator == 's') { double b = Double.parseDouble(strB); double c = sin(b); textField.setText("sin" + strB + "=" + c); } else if (operator == 'c') { double b = Double.parseDouble(strB); double c = cos(b); textField.setText("cos" + strB + "=" + c); } else if (operator == 't') { double b = Double.parseDouble(strB); double c = tan(b); textField.setText("tan" + strB + "=" + c); } else if (operator == '√') { double a = Double.parseDouble(strA); double c = sqrt(a); textField.setText(strA + "√" + "=" + c); } else if (operator == '^') { double a = Double.parseDouble(strA); double b = Double.parseDouble(strB); double c = pow(a, b); textField.setText(strA + "^" + strB + "=" + c); } else if (operator == '!') { int a = Integer.parseInt(strA); double c = f(a); textField.setText(strA + "!" + "=" + c); } else if (operator == 'm') { double a = Double.parseDouble(strA); double c = minusPower(a); textField.setText(strA + "(^-1)" + "=" + c); } operator = '~'; strA = ""; strB = ""; } } @Override public void mouseClicked(MouseEvent e) { textField.setText(""); operator = '~'; strA = ""; strB = ""; } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }