2014-12-13 69 views
0

你好,我有這個計算器,我希望它是無鍵的,就像在文本框中輸入用戶信息一樣,但我不知道如何使它像那樣工作。我想像這樣簡化它,但我不知道如何在沒有數字按鈕的情況下使其工作。沒有數字按鈕的GUI計算器

enter image description here

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

class MP2_2 extends JFrame implements ActionListener { 

Container c; 
JTextField result; 
JPanel p = new JPanel(); 
JButton b[] = new JButton[16]; 
String s[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "C"}; 
//----------------------Me------------------------ 
String Screen = "", monitor1 = "", monitor2 = "", OperationOnScreen = ""; 
boolean CommandEmpty = true, switcher = true; 
double R = Integer.MIN_VALUE, L = Integer.MIN_VALUE; 

//------------------------------------------------ 

public MP2_2() { 
    super("MP2_2"); 
    c = getContentPane(); 
    result = new JTextField(); 
    result.setEditable(true); 

    p.setLayout(new GridLayout(4, 4)); 
    for (int i = 0; i < 16; i++) { 
     b[i] = new JButton(s[i]); 
     b[i].addActionListener(this); 
     p.add(b[i]); 
    } 
    c.add(result, BorderLayout.NORTH); 
    c.add(p); 

}//End Constructor 

public static void main(String[] args) { 
    MP2_2 calcu = new MP2_2(); 
    calcu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    calcu.setSize(300, 300); 
    calcu.setVisible(true); 
} 

//------------------------------------------------    

public void actionPerformed(ActionEvent event) { 
    for (int i = 0; i <= 9; i++)//Numbers 
    { 
     if (event.getSource() == b[i]) { 
      Screen += i; 
      result.setText(""); 
      result.setText(Screen); 
     } 
    } 

    for (int i = 10; i <= 14; i++)//Commands 
    { 
     if (event.getSource() == b[i]) { 
      if (result.getText().lastIndexOf(OperationOnScreen) != -1)//prevent exception 
      { 
       result.setText(result.getText().substring(0, result.getText().lastIndexOf(OperationOnScreen)) + s[i]); 
      } else { 
       result.setText(result.getText() + s[i]); 
      } 
      OperationOnScreen = s[i]; 

      if (switcher) { 
       monitor1 = s[i]; 
       switcher = false; 
      } else { 
       monitor2 = s[i]; 
       switcher = true; 
      } 

      if (monitor1 != monitor2 && monitor2 != "") { 
       if (switcher) //execute older,send sign newer 
       { 
        Calc(event, monitor1.charAt(0), monitor2); 
       } else { 
        Calc(event, monitor2.charAt(0), monitor1); 
       } 
      } 
      if (s[i] != "=") //calc returns 0 
      { 
       Calc(event, s[i].charAt(0), s[i]); 
      } 
     } 
    } 

    if (event.getSource() == b[15]) //Clear 
    { 
     Screen = ""; 
     monitor1 = ""; 
     monitor2 = ""; 
     switcher = true; 
     CommandEmpty = true; 
     result.setText(""); 
    } 
}//end actionPerformed 

public void Calc(ActionEvent event, char OpType, String Operator) { 
    if (Operator == "=") { 
     Operator = ""; 
    } 

    if (CommandEmpty && Screen == "") { 
     return; 
    } else if (CommandEmpty && Screen != "") { 
     R = Integer.parseInt(Screen); 
     result.setText(Screen + Operator); 
     Screen = ""; 
     CommandEmpty = false; 
    } else if (!CommandEmpty && Screen != "") { 
     L = Integer.parseInt(Screen); 
     R = Operations(R, L, OpType);//calculate 
     Screen = ""; 
     result.setText(""); 
     result.setText(R + Operator); 
    } 
}//End Calc    

public static double Operations(double R, double L, char op) { 
    switch (op) { 
     case '+': 
      return R + L; 
     case '-': 
      return R - L; 
     case '*': 
      return R * L; 
     case '/': 
      return R/L; 
    } 
    return 0; 
} 
}//end class 

編輯:我想我的問題是不明確的遺憾。我希望我的程序也支持鍵盤輸入,我該怎麼做?所以我最終可以刪除圖片中的數字按鈕。

+0

*「但是我不知道如何讓它在沒有數字按鈕的情況下工作。」但是我不知道你的問題是什麼。你有問題嗎?順便說一句 - 如果我要走這個極簡主義的GUI路徑,那麼我會失去其餘的按鈕,並將這些內容交給'ScriptEngine'來評估用戶何時用回車鍵「結束」公式。 – 2014-12-13 14:46:56

回答

0

當您刪除按鈕時,需要通過調用result.getText()來恢復按鈕的操作。

下面是在您的數字按鈕上工作的代碼。它設置屏幕的值。

for (int i = 0; i <= 9; i++)//Numbers 
{ 
    if (event.getSource() == b[i]) { 
     Screen += i; 
     result.setText(""); 
     result.setText(Screen); 
    } 
} 

因此,當您點擊操作按鈕時,您需要設置屏幕。 這是您的命令循環。我加了Screen = result.getText();在else塊中。它爲我工作。

for (int i = 10; i <= 14; i++)//Commands 
{ 
    if (event.getSource() == b[i]) { 
     if (result.getText().lastIndexOf(OperationOnScreen) != -1)//prevent exception 
     { 
      result.setText(result.getText().substring(0, result.getText().lastIndexOf(OperationOnScreen)) + s[i]); 
     } else { 
      Screen = result.getText(); 
      result.setText(result.getText() + s[i]); 
     } 
+0

我需要重建哪些線才能使用它? – user827391012 2014-12-14 05:17:06

+0

這個問題目前還不清楚。如果您要求編譯Java代碼,則需要重新編譯整個文件。 – ProgrammersBlock 2014-12-14 15:28:52