Update src/main/java/dev/sillyangel/calc/Calculator.java

This commit is contained in:
2025-11-13 09:27:51 -06:00
parent a785234758
commit 3aec0e5471

View File

@@ -77,6 +77,8 @@ public class Calculator extends JFrame {
setContentPane(contentPane); setContentPane(contentPane);
display = new JTextField(); display = new JTextField();
display.setForeground(new Color(30, 144, 255));
display.setBackground(new Color(255, 255, 255));
display.setEditable(false); display.setEditable(false);
display.setFont(new Font("Tahoma", Font.PLAIN, 22)); display.setFont(new Font("Tahoma", Font.PLAIN, 22));
display.setColumns(10); display.setColumns(10);
@@ -102,133 +104,112 @@ public class Calculator extends JFrame {
); );
buttonPanel.setLayout(new GridLayout(0, 5, 0, 0)); buttonPanel.setLayout(new GridLayout(0, 5, 0, 0));
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn9);
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
buttonPanel.add(btnDiv);
JButton btnErase = new JButton("DEL");
btnErase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
}
});
buttonPanel.add(btnErase);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn6);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
JButton btnMul = new JButton("*");
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
buttonPanel.add(btnMul);
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
}
});
buttonPanel.add(btnClear);
buttonPanel.add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
buttonPanel.add(btn2);
buttonPanel.add(btn3);
JButton btn0 = new JButton("0"); JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6"); // why was 6 afraid of 7... because 7 8 9 :sob:
JButton btn7 = new JButton("7"); // 67!
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
btn0.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn0.addActionListener(new ActionListener() { btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand()); processDigit(e.getActionCommand());
} }
}); });
JButton btnPlus = new JButton("+"); btn1.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnPlus.addActionListener(new ActionListener() { btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand()); processDigit(e.getActionCommand());
} }
}); });
buttonPanel.add(btnPlus);
btn2.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn3.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn4.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn5.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn6.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn7.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn8.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn9.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
// other stuff
JButton btnErase = new JButton("DEL");
JButton empty = new JButton(""); JButton empty = new JButton("");
JButton btnClear = new JButton("C");
JButton btnPlusMin = new JButton("+/-");
JButton btnDot = new JButton(".");
btnErase.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnErase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
}
});
empty.setBackground(Color.WHITE); empty.setBackground(Color.WHITE);
empty.setEnabled(false); empty.setEnabled(false);
buttonPanel.add(empty);
JButton btnPlusMin = new JButton("+/-"); btnClear.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
}
});
btnPlusMin.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnPlusMin.addActionListener(new ActionListener() { btnPlusMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String tmp = display.getText(); String tmp = display.getText();
@@ -242,10 +223,8 @@ public class Calculator extends JFrame {
} }
} }
}); });
buttonPanel.add(btnPlusMin);
buttonPanel.add(btn0);
JButton btnDot = new JButton("."); btnDot.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnDot.addActionListener(new ActionListener() { btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String n = display.getText(); // get the text on the display String n = display.getText(); // get the text on the display
@@ -254,17 +233,60 @@ public class Calculator extends JFrame {
} }
}); });
buttonPanel.add(btnDot);
// math stuff
JButton btnDiv = new JButton("/");
JButton btnMul = new JButton("*");
JButton btnMin = new JButton("-"); JButton btnMin = new JButton("-");
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));
}
});
btnMul.setFont(new Font("Tahoma", Font.PLAIN, 29));
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));
}
});
btnMin.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnMin.addActionListener(new ActionListener() { btnMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand()); 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));
}
});
btnPlus.setFont(new Font("Tahoma", Font.PLAIN, 29));
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));
} }
}); });
buttonPanel.add(btnMin);
JButton btnEq = new JButton("="); JButton btnEq = new JButton("=");
btnEq.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnEq.addActionListener(new ActionListener() { btnEq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
operand2 = display.getText(); operand2 = display.getText();
@@ -283,10 +305,39 @@ public class Calculator extends JFrame {
operator = ""; operator = "";
operand1 = ""; operand1 = "";
operand2 = ""; 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; resultVisible = true;
display.setText(""+result); display.setText(""+result);
} }
}); });
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btnDiv);
buttonPanel.add(btnErase);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btnMul);
buttonPanel.add(btnClear);
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btnPlus);
buttonPanel.add(empty);
buttonPanel.add(btnPlusMin);
buttonPanel.add(btn0);
buttonPanel.add(btnDot);
buttonPanel.add(btnMin);
buttonPanel.add(btnEq); buttonPanel.add(btnEq);
contentPane.setLayout(gl_contentPane); contentPane.setLayout(gl_contentPane);