找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
尊貴會員無限使用任何功能尊貴會員無限下載附件尊貴會員無限看帖不用回覆
mega 無office無碼流出我的英雄出包王女柯南
新竹ntrmiab 155魔剣士リ1784119rustil e女兒

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[繁]怪異與少女與神隱

[繁]格林童話變奏曲02

[簡]單人房、日照一般

[繁]老夫老妻重返青春

[繁]從Lv2開始開外掛

[繁]Re:Monster 04-
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 1341|回復: 0
打印上一主題下一主題

[分享]發一個GUI的簡易計算機[複製鏈接]

Rank: 2Rank: 2

帖子
597
積分
700 點
潛水值
27566 米
跳轉到指定樓層
樓主
發表於 2012-2-20 03:55 PM|只看該作者|倒序瀏覽
回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。

這個計算機是我在學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. }
複製代碼


...
瀏覽完整內容,請先 註冊登入會員
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
分享分享0收藏收藏0支持支持0

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部