Update src/main/java/dev/sillyangel/calc/Calculator.java
This commit is contained in:
@@ -21,8 +21,6 @@ import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuItem;
|
||||
|
||||
|
||||
|
||||
public class Calculator extends JFrame implements KeyListener {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -31,6 +29,10 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
private String operand1;
|
||||
private String operator;
|
||||
private String operand2;
|
||||
private JButton btnDiv;
|
||||
private JButton btnMul;
|
||||
private JButton btnMin;
|
||||
private JButton btnPlus;
|
||||
protected boolean resultVisible;
|
||||
protected double result;
|
||||
|
||||
@@ -74,7 +76,6 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
exitItem.addActionListener(e -> System.exit(0));
|
||||
|
||||
setJMenuBar(menuBar);
|
||||
|
||||
|
||||
fileMenu.add(exitItem);
|
||||
menuBar.add(fileMenu);
|
||||
@@ -242,20 +243,15 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
});
|
||||
|
||||
// math stuff
|
||||
|
||||
JButton btnDiv = new JButton("/");
|
||||
JButton btnMul = new JButton("*");
|
||||
JButton btnMin = new JButton("-");
|
||||
JButton btnPlus = new JButton("+");
|
||||
|
||||
btnDiv = new JButton("/");
|
||||
btnMul = new JButton("*");
|
||||
btnMin = new JButton("-");
|
||||
btnPlus = new JButton("+");
|
||||
|
||||
btnDiv.setFont(new Font("Tahoma", Font.PLAIN, 29));
|
||||
btnDiv.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
btnDiv.setForeground(new Color(30, 144, 255));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -263,10 +259,6 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
btnMul.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(30, 144, 255));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -274,10 +266,6 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
btnMin.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(30, 144, 255));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -285,10 +273,6 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
btnPlus.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(30, 144, 255));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -296,30 +280,7 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
btnEq.setFont(new Font("Tahoma", Font.PLAIN, 29));
|
||||
btnEq.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
operand2 = display.getText();
|
||||
double o1 = Double.parseDouble(operand1);
|
||||
double o2 = Double.parseDouble(operand2);
|
||||
|
||||
if (operator == "+") {
|
||||
result = o1+o2;
|
||||
} else if (operator == "-") {
|
||||
result = o1-o2;
|
||||
} else if (operator == "*") {
|
||||
result = o1*o2;
|
||||
} else if (operator == "/") {
|
||||
result = o1/o2;
|
||||
}
|
||||
operator = "";
|
||||
operand1 = "";
|
||||
operand2 = "";
|
||||
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
|
||||
resultVisible = true;
|
||||
display.setText(""+result);
|
||||
math();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -363,34 +324,109 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
if (resultVisible == true) {
|
||||
display.setText("");
|
||||
resultVisible = false;
|
||||
}
|
||||
}
|
||||
if (actionCommand == "+") {
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(30, 144, 255));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
} else if (actionCommand == "*") {
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(30, 144, 255));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
} else if (actionCommand == "/") {
|
||||
btnDiv.setForeground(new Color(30, 144, 255));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
} else if (actionCommand == "-") {
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(30, 144, 255));
|
||||
}
|
||||
display.setText(display.getText() + actionCommand);
|
||||
|
||||
}
|
||||
protected void setOperator(String actionCommand) {
|
||||
protected void setOperator(String daop) {
|
||||
operand1 = display.getText();
|
||||
operator = actionCommand;
|
||||
operator = daop;
|
||||
display.setText("");
|
||||
}
|
||||
|
||||
|
||||
protected void math() {
|
||||
operand2 = display.getText();
|
||||
double op1 = Double.parseDouble(operand1);
|
||||
double op2 = Double.parseDouble(operand2);
|
||||
|
||||
if (operator == "+") {
|
||||
result = op1+op2;
|
||||
} else if (operator == "-") {
|
||||
result = op1-op2;
|
||||
} else if (operator == "*") {
|
||||
result = op1*op2;
|
||||
} else if (operator == "/") {
|
||||
result = op1/op2;
|
||||
} else {
|
||||
result = op2;
|
||||
System.out.println("Op: " + op1);
|
||||
System.out.println("Op2: " + op2);
|
||||
}
|
||||
|
||||
operator = "";
|
||||
operand1 = "";
|
||||
operand2 = "";
|
||||
|
||||
btnDiv.setForeground(new Color(0, 0, 0));
|
||||
btnMul.setForeground(new Color(0, 0, 0));
|
||||
btnPlus.setForeground(new Color(0, 0, 0));
|
||||
btnMin.setForeground(new Color(0, 0, 0));
|
||||
|
||||
resultVisible = true;
|
||||
display.setText(""+result);
|
||||
}
|
||||
|
||||
// Implement the keyPressed method
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
System.out.println("Key Pressed: " + KeyEvent.getKeyText(keyCode));
|
||||
// System.out.println("Key Pressed: " + KeyEvent.getKeyText(keyCode));
|
||||
if (KeyEvent.getKeyText(keyCode) == "Enter") {
|
||||
math();
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Implement the keyReleased method
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
System.out.println("Key Released: " + KeyEvent.getKeyText(keyCode));
|
||||
// int keyCode = e.getKeyCode();
|
||||
// System.out.println("Key Released: " + KeyEvent.getKeyText(keyCode));
|
||||
}
|
||||
|
||||
// Implement the keyTyped method
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
char keyChar = e.getKeyChar();
|
||||
System.out.println("Key Typed: " + keyChar);
|
||||
char c = e.getKeyChar();
|
||||
// System.out.println("Key Typed: " + keyChar);
|
||||
System.out.println(c + " KT");
|
||||
if (c >= '0' && c <= '9') {
|
||||
processDigit(String.valueOf(c));
|
||||
}
|
||||
if (c == '+') {
|
||||
setOperator("+");
|
||||
} else if (c == '*') {
|
||||
setOperator("*");
|
||||
} else if (c == '/') {
|
||||
setOperator("/");
|
||||
} else if (c == '-') {
|
||||
setOperator("-");
|
||||
}
|
||||
if (c == '=') {
|
||||
math();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user