[Java] java计算器 →→→→→进入此内容的聊天室

来自 , 2020-01-08, 写在 Java, 查看 119 次.
URL http://www.code666.cn/view/00e26af6
  1. import javax.swing.text.AttributeSet;  
  2. import javax.swing.text.BadLocationException;  
  3. import javax.swing.text.PlainDocument;  
  4.  
  5. public class MyDocument extends PlainDocument{  
  6.      int maxLength =20;  
  7.       public MyDocument(int newMaxLength){  
  8.           super();  
  9.           maxLength = newMaxLength;  
  10.       }  
  11.        public MyDocument(){  
  12.           this(20);  
  13.        }  
  14.        public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{  
  15.             if(getLength()+str.length()>maxLength){  
  16.                  return;  
  17.             }  
  18.             else{  
  19.                 super.insertString(offset,str,a);  
  20.  
  21.             }      
  22.           }  
  23. }  
  24.  
  25. import java.awt.Color;  
  26. import java.awt.Container;  
  27. import java.awt.GridLayout;  
  28. import java.awt.event.ActionEvent;  
  29. import java.awt.event.ActionListener;  
  30.  
  31. import javax.swing.JButton;  
  32. import javax.swing.JFrame;  
  33. import javax.swing.JLabel;  
  34. import javax.swing.JMenu;  
  35. import javax.swing.JMenuBar;  
  36. import javax.swing.JMenuItem;  
  37. import javax.swing.JOptionPane;  
  38. import javax.swing.JPanel;  
  39. import javax.swing.JTextField;  
  40.  
  41. public class CounterFrame extends JFrame implements ActionListener {  
  42.  
  43.     private static final long serialVersionUID = 1L;  
  44.     //获得内容面板  
  45.     private Container con=getContentPane();  
  46.     //创建菜单栏,菜单及菜单项  
  47.     private JMenuBar menuBar=new JMenuBar();  
  48.     private JMenu viewmenu=new JMenu("查看(V)");  
  49.     private JMenu helpmenu=new JMenu("帮助(H)");  
  50.     private JMenuItem general =new JMenuItem("标准型(T)",'T');  
  51.     private JMenuItem color =new JMenuItem("个性型(C)",'C');  
  52.     private JMenuItem about=new JMenuItem("关于(A)",'A');  
  53.     //创建一个容量为30个字符的文本框  
  54.     private JTextField textField=new JTextField(30);  
  55.     //创建JLabel  
  56.     private JLabel label=new JLabel();  
  57.     //创建计算器的各种按钮  
  58.     private JButton[] button=new JButton[27];  
  59.     private String[] buttonstr={"退格", "CE", "C", "MC", "7", "8", "9", "/", "sqrt",  
  60.             "MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x",  
  61.             "M+", "0", "+/-", ".", "+", "=" };  
  62.     //创建各种面板  
  63.     private JPanel panel=new JPanel();  
  64.     private JPanel childPanel=new JPanel();  
  65.     private JPanel childPanel1=new JPanel();  
  66.     private JPanel childPanel2=new JPanel();  
  67.     private JPanel childPanel3=new JPanel();  
  68.     private JPanel childPanel4=new JPanel();  
  69.     private JPanel childPanel5=new JPanel();  
  70.     private JPanel childPanel6=new JPanel();  
  71.     //创建布局管理器  
  72.     private GridLayout gridLayout=new GridLayout(6,1,6,6);  
  73.     private GridLayout gridLayout1=new GridLayout(1,3,6,6);  
  74.     private GridLayout gridLayout2=new GridLayout(1,6,6,6);  
  75.      
  76.     boolean tem=true; //标识0  
  77.     double  cache=0; //存储器  
  78.     boolean temd=true; //标识小数点  
  79.     boolean temm=false;//标识M+  
  80.     boolean plus=false;  //标识加  
  81.     boolean subtract=false; //标识减  
  82.     boolean mul=false; //标识乘  
  83.     boolean div=false; //标识除  
  84.     boolean rem=false; //百分号  
  85.      
  86.      
  87.     public CounterFrame(){  
  88.         viewmenu.setMnemonic('V');  
  89.         viewmenu.add(general);  
  90.         general.addActionListener(new ActionListener(){  
  91.  
  92.             @Override  
  93.             public void actionPerformed(ActionEvent e) {  
  94.                 childPanel.setBackground(Color.red);  
  95.                 childPanel1.setBackground(Color.red);  
  96.                 childPanel2.setBackground(Color.red);  
  97.                 childPanel3.setBackground(Color.red);  
  98.                 childPanel4.setBackground(Color.red);  
  99.                 childPanel5.setBackground(Color.red);  
  100.                 childPanel6.setBackground(Color.red);  
  101.                 panel.setBackground(Color.red);  
  102.             }  
  103.              
  104.         });  
  105.         viewmenu.add(color);  
  106.         color.addActionListener(new ActionListener(){  
  107.  
  108.             @Override  
  109.             public void actionPerformed(ActionEvent e) {  
  110.                 childPanel.setBackground(Color.blue);  
  111.                 childPanel1.setBackground(Color.cyan);  
  112.                 childPanel2.setBackground(Color.black);  
  113.                 childPanel3.setBackground(Color.darkGray);  
  114.                 childPanel4.setBackground(Color.green);  
  115.                 childPanel5.setBackground(Color.lightGray);  
  116.                 childPanel6.setBackground(Color.orange);  
  117.                 panel.setBackground(Color.red);  
  118.                  
  119.                  
  120.                  
  121.             }  
  122.              
  123.         });  
  124.         helpmenu.setMnemonic('H');  
  125.         helpmenu.add(about);  
  126.         about.addActionListener(new ActionListener(){  
  127.  
  128.             @Override  
  129.             public void actionPerformed(ActionEvent e) {  
  130.                  JOptionPane.showMessageDialog(CounterFrame.this,"孤风侠尘计算器1.0 作者:wustrive_2008",  
  131.                             " ",JOptionPane.INFORMATION_MESSAGE);  
  132.             }  
  133.              
  134.         });  
  135.          
  136.         menuBar.add(viewmenu);  
  137.         menuBar.add(helpmenu);  
  138.         this.setJMenuBar(menuBar);  
  139.          
  140.         for(int i=0;i<27;i++){  
  141.             button[i]=new JButton(buttonstr[i]);  
  142.             button[i].addActionListener(this);  
  143.         }  
  144.          
  145.         panel.setLayout(gridLayout);  
  146.         panel.add(childPanel1);  
  147.         childPanel1.add(label);  
  148.         childPanel1.add(textField);  
  149.         label.setBounds(10, 10, 10, 10);  
  150.         textField.setEditable(false);  
  151.         textField.setDocument(new MyDocument());  
  152.         textField.setText("0");  
  153.         textField.setBackground(Color.WHITE);  
  154.         textField.setHorizontalAlignment(JTextField.RIGHT);//设置文字从右边开始显示  
  155.          
  156.         panel.add(childPanel2);  
  157.         childPanel2.setLayout(gridLayout1);  
  158.         childPanel2.add(button[0]);  
  159.         childPanel2.add(button[1]);  
  160.         childPanel2.add(button[2]);  
  161.          
  162.         panel.add(childPanel3);  
  163.         childPanel3.setLayout(gridLayout2);  
  164.         childPanel3.add(button[3]);  
  165.         childPanel3.add(button[4]);  
  166.         childPanel3.add(button[5]);  
  167.         childPanel3.add(button[6]);  
  168.         childPanel3.add(button[7]);  
  169.         childPanel3.add(button[8]);  
  170.          
  171.         panel.add(childPanel4);  
  172.         childPanel4.setLayout(gridLayout2);  
  173.         childPanel4.add(button[9]);  
  174.         childPanel4.add(button[10]);  
  175.         childPanel4.add(button[11]);  
  176.         childPanel4.add(button[12]);  
  177.         childPanel4.add(button[13]);  
  178.         childPanel4.add(button[14]);  
  179.          
  180.         panel.add(childPanel5);  
  181.         childPanel5.setLayout(gridLayout2);  
  182.         childPanel5.add(button[15]);  
  183.         childPanel5.add(button[16]);  
  184.         childPanel5.add(button[17]);  
  185.         childPanel5.add(button[18]);  
  186.         childPanel5.add(button[19]);  
  187.         childPanel5.add(button[20]);  
  188.          
  189.         panel.add(childPanel6);  
  190.         childPanel6.setLayout(gridLayout2);  
  191.         childPanel6.add(button[21]);  
  192.         childPanel6.add(button[22]);  
  193.         childPanel6.add(button[23]);  
  194.         childPanel6.add(button[24]);  
  195.         childPanel6.add(button[25]);  
  196.         childPanel6.add(button[26]);  
  197.          
  198.          
  199.         childPanel.add(panel);  
  200.         con.add(childPanel);  
  201.          
  202.          
  203.         this.setResizable(false);  
  204.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  205.         this.setBounds(400, 300, 400, 300);  
  206.         this.setTitle("孤风侠尘计算器");  
  207.         this.setVisible(true);  
  208.          
  209.     }  
  210.          
  211.     //按钮事件处理  
  212.     @Override  
  213.     public void actionPerformed(ActionEvent e) {  
  214.         if(e.getSource()==button[4]){  
  215.             if(tem==false){  
  216.                 textField.setText(textField.getText()+buttonstr[4]);  
  217.             }  
  218.             else{  
  219.                 textField.setText(buttonstr[4]);  
  220.                 tem=false;  
  221.             }  
  222.         }  
  223.         if(e.getSource()==button[5]){  
  224.             if(tem==false){  
  225.                 textField.setText(textField.getText()+buttonstr[5]);  
  226.             }  
  227.             else{  
  228.                 textField.setText(buttonstr[5]);  
  229.                 tem=false;  
  230.             }  
  231.         }  
  232.         if(e.getSource()==button[6]){  
  233.             if(tem==false){  
  234.                 textField.setText(textField.getText()+buttonstr[6]);  
  235.             }  
  236.             else{  
  237.                 textField.setText(buttonstr[6]);  
  238.                 tem=false;  
  239.             }  
  240.         }  
  241.         if(e.getSource()==button[10]){  
  242.             if(tem==false){  
  243.                 textField.setText(textField.getText()+buttonstr[10]);  
  244.             }  
  245.             else{  
  246.                 textField.setText(buttonstr[10]);  
  247.                 tem=false;  
  248.             }  
  249.         }  
  250.         if(e.getSource()==button[11]){  
  251.             if(tem==false){  
  252.                 textField.setText(textField.getText()+buttonstr[11]);  
  253.             }  
  254.             else{  
  255.                 textField.setText(buttonstr[11]);  
  256.                 tem=false;  
  257.             }  
  258.         }  
  259.         if(e.getSource()==button[12]){  
  260.             if(tem==false){  
  261.                 textField.setText(textField.getText()+buttonstr[12]);  
  262.             }  
  263.             else{  
  264.                 textField.setText(buttonstr[12]);  
  265.                 tem=false;  
  266.             }  
  267.         }  
  268.         if(e.getSource()==button[16]){  
  269.             if(tem==false){  
  270.                 textField.setText(textField.getText()+buttonstr[16]);  
  271.             }  
  272.             else{  
  273.                 textField.setText(buttonstr[16]);  
  274.                 tem=false;  
  275.             }  
  276.         }  
  277.         if(e.getSource()==button[17]){  
  278.             if(tem==false){  
  279.                 textField.setText(textField.getText()+buttonstr[17]);  
  280.             }  
  281.             else{  
  282.                 textField.setText(buttonstr[17]);  
  283.                 tem=false;  
  284.             }  
  285.         }  
  286.         if(e.getSource()==button[18]){  
  287.             if(tem==false){  
  288.                 textField.setText(textField.getText()+buttonstr[18]);  
  289.             }  
  290.             else{  
  291.                 textField.setText(buttonstr[18]);  
  292.                 tem=false;  
  293.             }  
  294.         }  
  295.         if(e.getSource()==button[22]){  
  296.             if(tem==false){  
  297.                 textField.setText(textField.getText()+buttonstr[22]);  
  298.             }  
  299.             else{  
  300.                 textField.setText(buttonstr[22]);  
  301.                 //tem=false;  
  302.             }  
  303.         }  
  304.         if(e.getSource()==button[0]){  
  305.             if(textField.getText().length()>1){  
  306.                 textField.setText(textField.getText().substring(0,textField.getText().length()-1));  
  307.             }  
  308.             else{  
  309.                 textField.setText("0");  
  310.                 tem=true;  
  311.             }  
  312.         }  
  313.         //CE键:清除当前显示的数据  
  314.         if(e.getSource()==button[1]){  
  315.             textField.setText("0");  
  316.             tem=true;  
  317.             tem=true;  
  318.             temd=true;  
  319.             temm=false;  
  320.             plus=false;  
  321.             subtract=false;  
  322.             mul=false;  
  323.             div=false;  
  324.             rem=false;  
  325.              
  326.              
  327.         }  
  328.          
  329.         //C键:清除所有数据  
  330.         if(e.getSource()==button[2]){  
  331.             textField.setText("0");  
  332.             cache=0;  
  333.             tem=true;  
  334.             tem=true;  
  335.             cache=0;  
  336.             temd=true;  
  337.             temm=false;  
  338.             plus=false;  
  339.             subtract=false;  
  340.             mul=false;  
  341.             div=false;  
  342.             rem=false;  
  343.              
  344.         }  
  345.          
  346.         //MR键:将存于存储器中的数据显示出来  
  347.         if(e.getSource()==button[9]){  
  348.             textField.setText(""+cache+"");  
  349.         }  
  350.         //MC键:清除存储器中数据  
  351.         if(e.getSource()==button[3]){  
  352.             cache=0;  
  353.             label.setText("");  
  354.         }  
  355.          
  356.          
  357.         //MS键:将显示的数据存储在存储器中  
  358.         if(e.getSource()==button[15]){  
  359.             cache=Double.parseDouble(textField.getText());  
  360.         }  
  361.          
  362.         //M+键:将显示的数据与存储器中的数据相加并进行存储  
  363.         if(e.getSource()==button[21]){  
  364.                 label.setText("M");  
  365.                 cache=cache+Double.parseDouble(textField.getText());  
  366.                 temm=true;  
  367.              
  368.         }  
  369.         //处理小数点  
  370.         if(e.getSource()==button[24]){  
  371.             if(tem==false&&temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  372.                 textField.setText(textField.getText()+".");  
  373.                 temd=false;  
  374.             }  
  375.         }  
  376.         //处理1/x  
  377.         if(e.getSource()==button[20]){  
  378.             if(temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  379.                 textField.setText(""+1/Double.parseDouble(textField.getText())+"");  
  380.                 tem=true;  
  381.             }  
  382.         }  
  383.         //处理+/-  
  384.         if(e.getSource()==button[23]){  
  385.             if(temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  386.                 Double dou=Double.parseDouble(textField.getText());  
  387.                 dou=-dou;  
  388.                 textField.setText(""+dou+"");  
  389.                  
  390.             }  
  391.         }  
  392.         //除法  
  393.         if(e.getSource()==button[7]){  
  394.             if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  395.                 textField.setText(textField.getText()+"/");  
  396.                 div=true;  
  397.             }  
  398.         }  
  399.         //乘法  
  400.         if(e.getSource()==button[13]){  
  401.             if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  402.                 textField.setText(textField.getText()+"*");  
  403.                 mul=true;  
  404.             }  
  405.         }  
  406.         //百分数表示  
  407.         if(e.getSource()==button[14]){  
  408.             if((plus==true&&subtract==false&&mul==false&&div==false)||(plus==false&&subtract==true&&mul==false&&div==false)||  
  409.                     (plus==false&&subtract==false&&mul==true&&div==false)||(plus==false&&subtract==false&&mul==false&&div==true)){  
  410.                 if(plus==true){  
  411.                     int i=textField.getText().indexOf("+");  
  412.                     String substr1=textField.getText().substring(0,i);  
  413.                     String substr2=textField.getText().substring(i+1,textField.getText().length());  
  414.                     textField.setText(""+(Double.parseDouble(substr1)+Double.parseDouble(substr1)*Double.parseDouble(substr2)/100)+"");  
  415.                 }  
  416.                 if(subtract==true){  
  417.                     int i=textField.getText().indexOf("-");  
  418.                     String substr1=textField.getText().substring(0,i);  
  419.                     String substr2=textField.getText().substring(i+1,textField.getText().length());  
  420.                     textField.setText(""+(Double.parseDouble(substr1)-Double.parseDouble(substr1)*Double.parseDouble(substr2)/100)+"");  
  421.                 }  
  422.                 if(mul==true){  
  423.                     int i=textField.getText().indexOf("*");  
  424.                     String substr1=textField.getText().substring(0,i);  
  425.                     String substr2=textField.getText().substring(i+1,textField.getText().length());  
  426.                     textField.setText(""+(Double.parseDouble(substr1)*(Double.parseDouble(substr1)*Double.parseDouble(substr2)/100))+"");  
  427.                 }  
  428.                 if(subtract==true){  
  429.                     int i=textField.getText().indexOf("/");  
  430.                     String substr1=textField.getText().substring(0,i);  
  431.                     String substr2=textField.getText().substring(i+1,textField.getText().length());  
  432.                     textField.setText(""+(Double.parseDouble(substr1)/(Double.parseDouble(substr1)*Double.parseDouble(substr2)/100))+"");  
  433.                 }  
  434.             }  
  435.                  
  436.              
  437.         }  
  438.         //加法  
  439.         if(e.getSource()==button[25]){  
  440.             if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  441.                 textField.setText(textField.getText()+"+");  
  442.                 plus=true;  
  443.             }  
  444.         }  
  445.         //减法  
  446.         if(e.getSource()==button[19]){  
  447.             if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
  448.                 textField.setText(textField.getText()+"-");  
  449.                 subtract=true;  
  450.             }  
  451.         }  
  452.         //等于  
  453.         if(e.getSource()==button[26]){  
  454.             if(plus==true){  
  455.                 int i=textField.getText().indexOf("+");  
  456.                 String substr1=textField.getText().substring(0,i);  
  457.                 String substr2=textField.getText().substring(i+1,textField.getText().length());  
  458.                 if(substr2.length()==0){  
  459.                     textField.setText(""+substr1+"");  
  460.                 }  
  461.                 else{  
  462.                     double result=Double.parseDouble(substr1)+Integer.parseInt(substr2);  
  463.                     textField.setText(""+result+"");  
  464.                 }  
  465.                  
  466.                 plus=false;  
  467.                 tem=true;  
  468.             }  
  469.             if(subtract==true){  
  470.                 int i=textField.getText().indexOf("-");  
  471.                 String substr1=textField.getText().substring(0,i);  
  472.                 String substr2=textField.getText().substring(i+1,textField.getText().length());  
  473.                 if(substr2.length()==0){  
  474.                     textField.setText(""+substr1+"");  
  475.                 }  
  476.                 else{  
  477.                     double result=Double.parseDouble(substr1)-Integer.parseInt(substr2);  
  478.                     textField.setText(""+result+"");  
  479.                 }  
  480.                  
  481.                 subtract=false;  
  482.                 tem=true;  
  483.             }  
  484.             if(mul==true){  
  485.                 int i=textField.getText().indexOf("*");  
  486.                 String substr1=textField.getText().substring(0,i);  
  487.                 String substr2=textField.getText().substring(i+1,textField.getText().length());  
  488.                 if(substr2.length()==0){  
  489.                     textField.setText(""+substr1+"");  
  490.                 }  
  491.                 else{  
  492.                     double result=Double.parseDouble(substr1)*Integer.parseInt(substr2);  
  493.                     textField.setText(""+result+"");  
  494.                 }  
  495.                  
  496.                 mul=false;  
  497.                 tem=true;  
  498.             }  
  499.             if(div==true){  
  500.                 int i=textField.getText().indexOf("/");  
  501.                 String substr1=textField.getText().substring(0,i);  
  502.                 String substr2=textField.getText().substring(i+1,textField.getText().length());  
  503.                 if(substr2.length()==0){  
  504.                     textField.setText(""+substr1+"");  
  505.                 }  
  506.                 else if(Double.parseDouble(substr2)==0){  
  507.                     textField.setText("除数不能为零");  
  508.                 }  
  509.                 else{  
  510.                     double result=Double.parseDouble(substr1)/Double.parseDouble(substr2);  
  511.                     textField.setText(""+result+"");  
  512.                 }  
  513.                  
  514.                 div=false;  
  515.                 tem=true;  
  516.             }  
  517.         }  
  518.         //求平方根  
  519.         if(e.getSource()==button[8]){  
  520.             if(plus==false&&subtract==false&&mul==false&&rem==false&&div==false&&tem==false){  
  521.                 textField.setText(""+Math.sqrt(Double.parseDouble(textField.getText()))+"");  
  522.                 tem=true;  
  523.             }  
  524.              
  525.         }  
  526.     }  
  527.      
  528.  
  529. }  //源代码片段来自云代码http://yuncode.net
  530.  
  531.  
  532.  
  533. package Experience5;
  534.  
  535. import java.awt.BorderLayout;
  536. import java.awt.Component;
  537. import java.awt.GridBagConstraints;
  538. import java.awt.GridBagLayout;
  539. import java.awt.GridLayout;
  540. import java.awt.event.ActionEvent;
  541. import java.awt.event.ActionListener;
  542.  
  543.  
  544. import javax.swing.*;
  545.  
  546. public class SimpleCalculator extends JFrame implements ActionListener{
  547.         JTextField jText;
  548.         Boolean state=false;
  549.         double firstOperator,secondOperator;
  550.         int operate;
  551.         int count=0;
  552.        
  553.         //控件的定义
  554.         JPanel jUpPanel;
  555.         JPanel jDownPanel;
  556.         GridBagLayout gbLayout;
  557.         GridBagConstraints gbConstraints;
  558.         String[] jButNames={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
  559.         JButton[] jButs =new JButton[16];
  560.        
  561.         public static void main(String args[])
  562.         {
  563.                 SimpleCalculator calculator=new SimpleCalculator();
  564.         }
  565.        
  566.         public SimpleCalculator()
  567.         {
  568.                 super("A Simple Calculator");
  569.                
  570.  
  571.                 //窗体的设定
  572.                 this.setSize(400, 300);
  573.                 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  574.                 //this.pack();
  575.                 this.setVisible(true);
  576.                
  577.                 //框架的定义
  578.                 jUpPanel=new JPanel();                 
  579.                 jDownPanel=new JPanel();
  580.                 gbLayout=new GridBagLayout();
  581.                 gbConstraints=new GridBagConstraints();
  582.                 this.getContentPane().setLayout(gbLayout);
  583.                
  584.                 gbConstraints.weightx = 1;
  585.                 gbConstraints.weighty = 0;
  586.                 gbConstraints.fill = GridBagConstraints.BOTH;
  587.                 gbConstraints.gridx=0;
  588.                 gbConstraints.gridy=0;
  589.                 gbConstraints.gridheight=1;
  590.                 gbConstraints.gridwidth=4;
  591.                 gbLayout.setConstraints(jUpPanel, gbConstraints);
  592.                 this.getContentPane().add(jUpPanel);
  593.                 gbConstraints.weighty = 1;
  594.                 gbConstraints.gridx=0;
  595.                 gbConstraints.gridy=1;
  596.                 gbConstraints.gridheight=4;
  597.                 gbConstraints.gridwidth=4;
  598.                 gbLayout.setConstraints(jDownPanel, gbConstraints);    
  599.                 this.getContentPane().add(jDownPanel);
  600.                 /*
  601.                 gbConstraints.weightx = 1;
  602.                 gbConstraints.weighty = 0;
  603.                 gbConstraints.fill = GridBagConstraints.BOTH;
  604.                 addComponent(jUpPanel, gbLayout, gbConstraints, 0, 0, 4, 1);
  605.                 gbConstraints.weighty = 1;
  606.                 addComponent(jDownPanel, gbLayout, gbConstraints, 1, 0, 4, 4); 
  607.                 this.getContentPane().setLayout(gbLayout);
  608.                 */
  609.  
  610.                
  611.                 //jUpPanel内容的填充
  612.                 jText=new JTextField();
  613.                 jText.setHorizontalAlignment(JTextField.RIGHT);
  614.                 jText.setEnabled(false);
  615.                 jText.setText("本计算器支持连续操作,即1+3-4+5=这样的操作");
  616.                 jUpPanel.setLayout(new BorderLayout());
  617.                 jUpPanel.add(jText);
  618.                
  619.                 //jDownPanel内容的填充
  620.                 jDownPanel.setLayout(new GridLayout(4,4));
  621.                 for(int i=0;i<16;i++)
  622.                 {
  623.                         jButs[i]=new JButton(jButNames[i]);    
  624.                         jButs[i].addActionListener(this);
  625.                         jButs[i].setActionCommand(jButs[i].getName());
  626.                         jDownPanel.add(jButs[i]);
  627.                 }
  628.                
  629.         }
  630.  
  631.         @Override
  632.         public void actionPerformed(ActionEvent e) {
  633.                 // TODO Auto-generated method stub
  634.                
  635.                 if(e.getSource()==jButs[3]||e.getSource()==jButs[7]||e.getSource()==jButs[11]||e.getSource()==jButs[15])
  636.                 {
  637.                         double temp=0.0;
  638.                         count++;
  639.                         try
  640.                         {
  641.                                 temp=Double.parseDouble(jText.getText());
  642.                         }
  643.                         catch(Exception ex)
  644.                         {
  645.                                 ex.printStackTrace();
  646.                                 JOptionPane.showMessageDialog((Component) e.getSource(), "第一个操作数不正确");
  647.                                 System.exit(0);
  648.                         }
  649.                         if(count>1)
  650.                         {
  651.                                 if(operate==1)
  652.                                         temp=firstOperator/temp;
  653.                                 else if(operate==2)
  654.                                         temp=firstOperator*temp;
  655.                                 else if(operate==3)
  656.                                         temp=firstOperator-temp;
  657.                                 else
  658.                                         temp=firstOperator+temp;
  659.                                 jText.setText(Double.toString(temp));
  660.                                 state=false;
  661.                         }
  662.                         else
  663.                         {
  664.                                 jText.setText("");
  665.                         }
  666.                         if(e.getSource()==jButs[3])
  667.                                 operate=1;
  668.                         else if(e.getSource()==jButs[7])
  669.                                 operate=2;
  670.                         else if(e.getSource()==jButs[11])
  671.                                 operate=3;
  672.                         else
  673.                                 operate=4;
  674.                         firstOperator=temp;
  675.                 }
  676.                 else if(e.getSource()==jButs[14])
  677.                 {
  678.                         double answer;
  679.                         count=0;
  680.                         try
  681.                         {
  682.                                 secondOperator=Double.parseDouble(jText.getText());
  683.                         }
  684.                         catch(Exception ex)
  685.                         {
  686.                                 ex.printStackTrace();
  687.                                 JOptionPane.showMessageDialog((Component) e.getSource(), "第二个操作数不正确");
  688.                                 System.exit(0);
  689.                         }
  690.                        
  691.                         if(operate==1)
  692.                                 answer=firstOperator/secondOperator;
  693.                         else if(operate==2)
  694.                                 answer=firstOperator*secondOperator;
  695.                         else if(operate==3)
  696.                                 answer=firstOperator-secondOperator;
  697.                         else
  698.                                 answer=firstOperator+secondOperator;
  699.                         jText.setText(Double.toString(answer));
  700.                         state=false;
  701.                 }
  702.                 else
  703.                 {
  704.                         if(state)
  705.                                 jText.setText(jText.getText()+e.getActionCommand());
  706.                         else
  707.                         {
  708.                                 state=true;
  709.                                 jText.setText(e.getActionCommand());
  710.                                
  711.                         }
  712.                 }
  713.         }
  714.        
  715.         /*
  716.         private void addComponent(Component c, GridBagLayout g,
  717.                         GridBagConstraints gc, int row, int column, int width, int height) {
  718.                                 gc.gridx = column;
  719.                                 gc.gridy = row;
  720.                                 gc.gridwidth = width;
  721.                                 gc.gridheight = height;
  722.                                 g.setConstraints(c, gc);
  723.                                 add(c);
  724.                 }
  725.         */
  726.        
  727.  
  728.  
  729. }
  730. //源代码片段来自云代码http://yuncode.net
  731.                        
  732.                        

回复 "java计算器"

这儿你可以回复上面这条便签

captcha