伊莉討論區

標題: 發一個GUI的簡易計算機 [打印本頁]

作者: 闇冰    時間: 2012-2-20 03:55 PM     標題: 發一個GUI的簡易計算機


這個計算機是我在學GUI時寫出來的
但寫完之後才發現...
沒有加上ESC的功能==
記得的時候都太遲了...
因為很想睡的關係就沒有再更新了...
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.Label;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;


  11. public class FracCalc implements ActionListener {
  12.         int fill[] = {
  13.                         GridBagConstraints.BOTH,
  14.                         GridBagConstraints.VERTICAL,
  15.                       GridBagConstraints.HORIZONTAL,
  16.                       GridBagConstraints.NONE };
  17.         int anchor[] = { GridBagConstraints.CENTER,
  18.                         GridBagConstraints.EAST,
  19.                         GridBagConstraints.SOUTH,
  20.                         GridBagConstraints.SOUTHEAST,
  21.                         GridBagConstraints.SOUTHWEST,
  22.                         GridBagConstraints.WEST,
  23.                         GridBagConstraints.NORTH,
  24.                         GridBagConstraints.NORTHEAST,
  25.                         GridBagConstraints.NORTHWEST };
  26.         int att[] [] = { {0, 0, 4, 1, fill[2], anchor[0],1, 1, 0, 0, 3, 0},  // use GridBagLayout
  27.                                          {0, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  28.                                          {1, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  29.                                          {2, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  30.                                          {3, 1, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  31.                                          {0, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  32.                                          {1, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  33.                                          {2, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  34.                                          {3, 2, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  35.                                          {0, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  36.                                          {1, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  37.                                          {2, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  38.                                          {3, 3, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  39.                                          {0, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  40.                                          {1, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  41.                                          {2, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  42.                                          {3, 4, 1, 1, fill[0], anchor[0],1, 1, 1, 1, 1, 1},
  43.                                          };
  44.         JFrame frame = new JFrame("calculater");
  45.         Label text=new Label("0",Label.RIGHT);
  46.         JButton button[]={  new JButton("7"),new JButton("8"),  new JButton("9"),new JButton("+"),
  47.                                                 new JButton("4"),new JButton("5"),  new JButton("6"),new JButton("-"),
  48.                                                 new JButton("1"),new JButton("2"),  new JButton("3"),new JButton("*"),
  49.                                                 new JButton("0"),new JButton("."),        new JButton("="),new JButton("/"),
  50.                 };
  51.         private void add(JFrame jf, Component b, int a[]){  //
  52.                 GridBagConstraints cons = new GridBagConstraints();
  53.                 cons.gridx = a[0];       // X
  54.                 cons.gridy = a[1];                 // Y
  55.                 cons.gridwidth = a[2];         //
  56.                 cons.gridheight = a[3];  //
  57.                 cons.fill = a[4];                 //
  58.                 cons.anchor = a[5];                 //
  59.                 cons.weightx = a[6];         //
  60.                 cons.weighty = a[7];         //
  61.                 cons.insets = new Insets(a[8], a[9], a[10], a[11]); //
  62.                 jf.add(b, cons);             //
  63.         }
  64.        
  65.         char tempChar = '+'; // save +-*/
  66.         boolean specialChar = true; // to know +-*/
  67.         String nowNumber = "0"; // show 0 in the text when it start
  68.         double tempNumber = 0; // the number for now
  69.         Boolean point = false; // "."
  70.         Boolean typeTF = false; // to find there are word to enter
  71.        
  72.         public FracCalc() {
  73.                 frame.setLayout(new GridBagLayout()); // using GridBagLayout to put things on the frame
  74.                 text.setBackground(new Color (0xFF,0xFF,0xCC)); // color of the text
  75.                 text.setText("0"); // label shows "0"
  76.                 add(frame, text, att[0]); // put text in the frame
  77.                 for (int i=0; i<button.length; i++){ // use for to put the button in the frame
  78.                         add(frame, button[i], att[i+1]);
  79.                         button[i].addActionListener(this);
  80.                 }
  81.                 frame.pack(); // make it shows the button and text when it start
  82.                 frame.setVisible(true); // show the frame
  83.         }

  84.         public static void main(String[] args) {
  85.                 new FracCalc();
  86.         }
  87.        
  88.         private void compute(String num){ // for compute
  89.                 point = cmoparePoint(); // is a point already there or not
  90.                 char [] nextNumber = num.toCharArray(); // make the char to array
  91.                 if(!typeTF && (nextNumber[0] >= '0' && nextNumber[0] <= '9')){ // make the nowNumber become 0 if no word enter in
  92.                 nowNumber = ""; // clear nowNumber
  93.                 }
  94.                 switch(nextNumber[0]){
  95.                 case '1':
  96.                         changeNowNumber("1");  //change label number
  97.                         break;
  98.                 case '2':
  99.                         changeNowNumber("2");
  100.                         break;
  101.                 case '3':
  102.                         changeNowNumber("3");
  103.                         break;
  104.                 case '4':
  105.                         changeNowNumber("4");
  106.                         break;
  107.                 case '5':
  108.                         changeNowNumber("5");
  109.                         break;
  110.                 case '6':
  111.                         changeNowNumber("6");
  112.                         break;
  113.                 case '7':
  114.                         changeNowNumber("7");
  115.                         break;
  116.                 case '8':
  117.                         changeNowNumber("8");
  118.                         break;
  119.                 case '9':
  120.                         changeNowNumber("9");
  121.                         break;       
  122.                 case '0':
  123.                         if(!nowNumber.equalsIgnoreCase("0")){
  124.                                 changeNowNumber("0");
  125.                         }
  126.                         break;
  127.                 default:
  128.                         // is special char and some number to enter in
  129.                         if(specialChar && typeTF){
  130.                                 specialChar = false;
  131.                                 switch(tempChar){
  132.                                 case '/':
  133.                                         if(tempNumber != 0){
  134.                                                 tempNumber /= Double.parseDouble(text.getText());
  135.                                         }
  136.                                         break;
  137.                                 case '*':
  138.                                         tempNumber *= Double.parseDouble(text.getText());
  139.                                         break;
  140.                                 case '+':
  141.                                         tempNumber += Double.parseDouble(text.getText());
  142.                                         break;
  143.                                 case '-':
  144.                                         tempNumber -= Double.parseDouble(text.getText());
  145.                                         break;
  146.                                 }
  147.                                 text.setText("" + tempNumber);
  148.                                 typeTF = false;
  149.                         }
  150.                         else{
  151.                                 nowNumber = "";
  152.                         }
  153.                         specialChar = true;
  154.         }
  155.         // special char
  156.         if(specialChar){
  157.                 switch(nextNumber[0]){
  158.                         case '/':
  159.                                 tempChar = '/';
  160.                                 tempNumber = Double.parseDouble(text.getText());
  161.                                 break;
  162.                         case '*':
  163.                                 tempChar = '*';
  164.                                 tempNumber = Double.parseDouble(text.getText());
  165.                                 break;
  166.                         case '+':
  167.                                 tempChar = '+';
  168.                                 tempNumber = Double.parseDouble(text.getText());
  169.                                 break;
  170.                         case '-':
  171.                                 tempChar = '-';
  172.                                 tempNumber = Double.parseDouble(text.getText());
  173.                                 break;
  174.                         case '=':
  175.                                 tempChar = '=';
  176.                                 text.setText("" + tempNumber);
  177.                                 break;
  178.                 }
  179.         }
  180. }
  181.         public void changeNowNumber(String nowNum){
  182.                 if(nowNumber.equals("0")){
  183.                         nowNumber="";
  184.                 }
  185.                 nowNumber+=nowNum;
  186.                 text.setText(""+nowNumber);
  187.                 typeTF=true;
  188.         }
  189.         public Boolean cmoparePoint(){ // define there are the point or more than one point
  190.                 char str[] = nowNumber.toCharArray();
  191.                 for (int i=0;i<str.length;i++){
  192.                         if (str[i]=='.'){return true;}
  193.                 }
  194.                 return false;
  195.         }
  196.         public void actionPerformed(ActionEvent e) {
  197.                 JButton number= (JButton)e.getSource();
  198.                 compute(number.getText());
  199.         }
  200. }
複製代碼
[attach]70747971[/attach]






歡迎光臨 伊莉討論區 (http://s03.p02.eyny.com/) Powered by Discuz!