java swing實現簡單計算機

來源:互聯網
上載者:User

 

代碼重複率太高了,應該最佳化一下的,沒時間了,呵呵。

setActionCommand() 方法可以大大減少代碼量。

Code:
  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import javax.swing.*;   
  4.   
  5. public class Test1 {   
  6.     public static void main(String[] args) {   
  7.         Calculator c = new Calculator();   
  8.         c.setVisible(true);   
  9.     }   
  10. }   
  11.   
  12. class Calculator extends JFrame implements ActionListener {   
  13.     Container p;   
  14.     JSplitPane jsp;   
  15.     JTextField resultFiled = new JTextField("0");   
  16.     JPanel resultPane = new JPanel();   
  17.     JPanel btnPane = new JPanel();   
  18.     static float op1=0;   
  19.     static int len=0;   
  20.     static float op2=0;   
  21.     static String output = "";   
  22.     static String opp = "";   
  23.     static boolean flag1 = true;   
  24.     static boolean flag2 = true;   
  25.     String[] btnS = {"1", "2", "3", "+",   
  26.                                      "4", "5", "6", "-",   
  27.                                      "7", "8", "9", "*",   
  28.                                      "0", "=", ".", "/"};   
  29.     JButton[] btn = new JButton[16];   
  30.        
  31.     public Calculator() {   
  32.         p = getContentPane();   
  33.         setBounds(100, 100, 300, 300);   
  34.         setTitle("計算機");   
  35.         setResizable(false);   
  36.         setIconImage(new ImageIcon("16.png").getImage());    //設定表單顯示的表徵圖   
  37.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  38.         setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  //設定滑鼠形狀   
  39.         this.setBackground(new Color(255, 0, 255));   
  40.         JButton cz = new JButton("reset");   
  41.         cz.setBounds(0,0,80,20);   
  42.         cz.setBackground(new Color(65, 105, 225));   
  43.         cz.setForeground(new Color(255, 0, 0));   
  44.         cz.addActionListener(this);   
  45.         resultFiled.setEditable(false);   
  46.         resultFiled.setHorizontalAlignment(JTextField.RIGHT);  //設定自右向左顯示   
  47.         resultFiled.setColumns(12);   
  48.         resultFiled.setFont(new Font("", Font.PLAIN, 14));   
  49.         resultFiled.setBackground(new Color(0, 255, 127));   
  50.         resultFiled.setForeground(new Color(255, 0, 0));   
  51.         JButton qx = new JButton("cancel");   
  52.         qx.addActionListener(this);   
  53.         qx.setBackground(new Color(65, 105, 225));   
  54.         qx.setForeground(new Color(255, 0, 0));   
  55.         qx.setSize(80,20);   
  56.         resultPane.setLayout(new FlowLayout());   
  57.         resultPane.add(cz);   
  58.         resultPane.add(resultFiled);   
  59.         resultPane.add(qx);   
  60.         btnPane.setLayout(new GridLayout(4, 5, 7, 7));   
  61.         for(int i=0; i<16; i++) {   
  62.             btn[i] = new JButton(btnS[i]);   
  63.             btn[i].setBorderPainted(true);   
  64.             btn[i].setContentAreaFilled(true);   
  65.             btn[i].setFont(new Font("", Font.BOLD, 25));   
  66.             btn[i].setBackground(new Color(255,255,0));   
  67.             btn[i].setForeground(new Color(255,0,0));   
  68.             btn[i].addActionListener(this);   
  69.             btnPane.add(btn[i]);   
  70.         }   
  71.         jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, resultPane, btnPane);   
  72.         jsp.setDividerSize(3);   
  73.         jsp.setDividerLocation(35);   
  74.         p.add(jsp, BorderLayout.CENTER);   
  75.     }   
  76.     public void actionPerformed(ActionEvent e) {   
  77.         if(flag1&&   
  78.           (e.getActionCommand()=="1" ||   
  79.            e.getActionCommand()=="2" ||   
  80.            e.getActionCommand()=="3" ||   
  81.            e.getActionCommand()=="4" ||   
  82.            e.getActionCommand()=="5" ||   
  83.            e.getActionCommand()=="6" ||   
  84.            e.getActionCommand()=="7" ||   
  85.            e.getActionCommand()=="8" ||   
  86.            e.getActionCommand()=="9" ||   
  87.            e.getActionCommand()=="0" ||   
  88.            e.getActionCommand()==".")) {   
  89.             output += e.getActionCommand();   
  90.             resultFiled.setText(output);   
  91.         }   
  92.         if(e.getActionCommand()=="=") {   
  93.             if(flag1){   
  94.                 op2 = Float.parseFloat(resultFiled.getText().substring(len+1));   
  95.                 output += e.getActionCommand();   
  96.                 if(opp == "+") {   
  97.                     output += (op1+op2)+"";   
  98.                 } if(opp == "-") {   
  99.                     output += (op1+op2)+"";   
  100.                 } if(opp == "*") {   
  101.                     output += (op1*op2)+"";   
  102.                 } if(opp == "/"){   
  103.                     if(op2-0.0 == 0) {   
  104.                         output += "error";   
  105.                     } else {   
  106.                         output += (op1/op2)+"";   
  107.                     }   
  108.                 }   
  109.                 resultFiled.setText(output);   
  110.                 flag1 = false;   
  111.             }   
  112.         }   
  113.         if(e.getActionCommand() == "reset") {   
  114.             op1 = 0;   
  115.             op2 = 0;   
  116.             opp = "";   
  117.             output ="";   
  118.             flag1 = true;   
  119.             flag2 = true;   
  120.             resultFiled.setText("0");   
  121.         }   
  122.         if(e.getActionCommand() == "+" ||   
  123.            e.getActionCommand() == "-" ||   
  124.            e.getActionCommand() == "*" ||   
  125.            e.getActionCommand() == "/"){   
  126.             if(flag2) {   
  127.                 op1 = Float.parseFloat(resultFiled.getText());   
  128.                 len = resultFiled.getText().length();   
  129.                 opp = e.getActionCommand();   
  130.                 output += e.getActionCommand();   
  131.                 resultFiled.setText(output);   
  132.                 flag2 = false;   
  133.             }   
  134.         }              
  135.     }   
  136. }  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.