[英]GUI Calculator with no number buttons
您好,我有這個計算器,我希望它是無按鈕的,就像在文本字段中輸入用戶輸入一樣,但是我不知道如何使它像這樣工作。 我想這樣簡化它,但是我不知道如何在沒有數字按鈕的情況下使它工作。
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
編輯:我想我的問題不是很抱歉。 我希望我的程序也支持鍵盤輸入,該怎么辦? 因此,我最終可以刪除圖片中的數字按鈕。
刪除按鈕時,需要通過調用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]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.