当前位置:文档之家› java简单界面计算器

java简单界面计算器

package calculator;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;/**** @author 某某某**/public class Calculator extends JFrame implements ActionListener{//设计按钮数组1包含数字键和删除键DEL和清屏键,小数点。

JButton[] but1 = {new JButton("1"),new JButton("2"),new JButton("3"),new JButton("4"),new JButton("5"),new JButton("6"),new JButton("7"),new JButton("8"),new JButton("9"),new JButton("."),new JButton("0"),new JButton("del")};//设计按钮数组2包含加减乘除,开根号,取余,等于键,平方,三次方。

JButton[] but2 = {new JButton("+"),new JButton("-"),new JButton("*"),new JButton("/"),new JButton("^2"),new JButton("^3"),new JButton("%"),new JButton("sqrt"),new JButton("exit"),new JButton("="),new JButton("AC")} ;//创建一个单行文本JTextField text = new JTextField(25);//创建五个面板JPanel jp1 = new JPanel();JPanel jp2 = new JPanel();JPanel jp3 = new JPanel();JPanel jp4 = new JPanel();JPanel jp5 = new JPanel();//创建有关状态变量boolean clicked = true;//是否有小数点boolean clear = true;//是否应该清屏double num1,num2;//两次输入的数字String sign;//用户点击的运算符号//主要界面设计public Calculator(){this.setVisible(true);this.setTitle("my calculator");this.setSize(300,250);this.setLocation(400, 400);text.setHorizontalAlignment(JTextField.RIGHT);//设计各面板的格式jp1.setLayout(new GridLayout(1,1));jp2.setLayout(new GridLayout(4,3));jp3.setLayout(new GridLayout(4,1));jp4.setLayout(new GridLayout(4,1));jp5.setLayout(new GridLayout(1,1));//设置整体布局//面板一种只有一个单行文本放在NORTH。

this.add(jp1,BorderLayout.NORTH);jp1.add(text);text.setEditable(false);//面板二是数字面板还包括小数点和Del键,放在CENTER this.add(jp2,BorderLayout.CENTER);//循环向面板二中添加按钮,并添加监听者for (int i = 0; i < but1.length; i++) {jp2.add(but1[i]);but1[i].addActionListener(this);}//面板三是加减乘除,放在EASTthis.add(jp3,BorderLayout.EAST);//循环向面板中添加按钮,并添加监听者for (int i = 0; i < 4; i++) {jp3.add(but2[i]);but2[i].addActionListener(this);}//面板四是平方,三次方,取余,开方,放在WEST。

this.add(jp4,BorderLayout.WEST);//循环向里面添加按钮,并添加监听者for (int i = 4; i < 8; i++) {jp4.add(but2[i]);but2[i].addActionListener(this);}//面板五是exit键,等于键,和清屏键,放在SOUTH。

this.add(jp5,BorderLayout.SOUTH);//循环向其中添加按钮,并添加监听者for (int i = 8; i < but2.length; i++) {jp5.add(but2[i]);but2[i].addActionListener(this);}}//监听设置public void actionPerformed(ActionEvent e) {//del键if(e.getActionCommand() == "del"){String s = text.getText();text.setText(null);for (int i = 0; i < s.length()-1; i++) {char a = s.charAt(i);text.setText(text.getText() + a);}}//AC键(清零)if(e.getActionCommand() == "AC"){text.setText(null);clear = true;}if(e.getActionCommand() == "0"){ if(clear == false)text.setText(null);text.setText(text.getText()+"0");clear = true;}if(e.getActionCommand() == "1"){ if(clear == false)text.setText(null);text.setText(text.getText()+"1");clear = true;}if(e.getActionCommand() == "2"){ if(clear == false)text.setText(null);text.setText(text.getText()+"2");clear = true;}if(e.getActionCommand() == "3"){ if(clear == false)text.setText(null);text.setText(text.getText()+"3");clear = true;}if(e.getActionCommand() == "4"){ if(clear == false)text.setText(null);text.setText(text.getText()+"4");clear = true;}if(e.getActionCommand() == "5"){ if(clear == false)text.setText(null);text.setText(text.getText()+"5");clear = true;}if(e.getActionCommand() == "6"){ if(clear == false)text.setText(null);text.setText(text.getText()+"6");clear = true;}if(e.getActionCommand() == "7"){if(clear == false)text.setText(null);text.setText(text.getText()+"7");clear = true;}if(e.getActionCommand() == "8"){if(clear == false)text.setText(null);text.setText(text.getText()+"8");clear = true;}if(e.getActionCommand() == "9"){if(clear == false)text.setText(null);text.setText(text.getText()+"9");clear = true;}//小数点键if(e.getActionCommand() == "."){clicked = true;for (int i = 0; i < text.getText().length(); i++) {if('.' == text.getText().charAt(i)){clicked = false;break;}}if(clicked == true)text.setText(text.getText()+".");}//加减乘除键if(e.getActionCommand() == "+"){if(clear == true){num1 = Double.parseDouble(text.getText());text.setText(null);}text.setText(text.getText()+"+");sign = "+";clear = false;}if(e.getActionCommand() == "-"){num1 = Double.parseDouble(text.getText());sign = "-";clear = false;}if(e.getActionCommand() == "*"){num1 = Double.parseDouble(text.getText());sign = "*";clear = false;}if(e.getActionCommand() == "/"){num1 = Double.parseDouble(text.getText());sign = "/";clear = false;}//等于键if(e.getActionCommand() == "="){num2 = Double.parseDouble(text.getText());text.setText(null);if(sign == "+")text.setText(num1 + num2+"");if(sign == "-")text.setText(num1 - num2+"");if(sign == "*")text.setText(num1 * num2+"");if(sign == "/")if(num2 == 0)text.setText("除数不能为零");elsetext.setText(num1 / num2+"");if(sign == "%")if(num2 == 0)text.setText("error!");elsetext.setText(num1 % num2+"");clear = false;}if(e.getActionCommand() == "sqrt"){String s = text.getText();if(s.charAt(0)=='-')text.setText("根号下的数不能小于零");elsetext.setText(Double.toString(Math.sqrt(Double.parseDouble(text.getText()))));clear = false;}if(e.getActionCommand() == "%"){num1 = Double.parseDouble(text.getText());sign = "%";clear = false;}if(e.getActionCommand() == "^2"){num1 = Double.parseDouble(text.getText());text.setText(num1*num1+"");}if(e.getActionCommand() == "^3"){num1 = Double.parseDouble(text.getText());text.setText(num1*num1*num1+"");}if(e.getActionCommand() == "exit"){System.exit(0);}}public static void main(String[] args) {Calculator calculator = new Calculator();}}。

相关主题