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

@@ -1,317 +1,368 @@
package dev.sillyangel.calc; package dev.sillyangel.calc;
import java.awt.EventQueue; import java.awt.EventQueue;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.GroupLayout; import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment; import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField; import javax.swing.JTextField;
import java.awt.GridLayout; import java.awt.GridLayout;
import javax.swing.JButton; import javax.swing.JButton;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.Color; import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.Toolkit; import java.awt.Toolkit;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class Calculator extends JFrame { public class Calculator extends JFrame {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private JPanel contentPane; private JPanel contentPane;
private JTextField display; private JTextField display;
private String operand1; private String operand1;
private String operator; private String operator;
private String operand2; private String operand2;
protected boolean resultVisible; protected boolean resultVisible;
protected double result; protected double result;
/** /**
* Launch the application. * Launch the application.
*/ */
public static void main(String[] args) { public static void main(String[] args) {
try { try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
try { try {
Calculator frame = new Calculator(); Calculator frame = new Calculator();
frame.setVisible(true); frame.setVisible(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}); });
} }
/** /**
* Create the frame. * Create the frame.
*/ */
public Calculator() { public Calculator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon.png"))); setIconImage(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon.png")));
setTitle("Calc (short for calculator)"); setTitle("Calc (short for calculator)");
setBounds(100, 100, 634, 553); setBounds(100, 100, 634, 553);
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File"); JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit"); JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0)); exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitItem); fileMenu.add(exitItem);
menuBar.add(fileMenu); menuBar.add(fileMenu);
setJMenuBar(menuBar); setJMenuBar(menuBar);
contentPane = new JPanel(); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane); setContentPane(contentPane);
display = new JTextField(); display = new JTextField();
display.setEditable(false); display.setForeground(new Color(30, 144, 255));
display.setFont(new Font("Tahoma", Font.PLAIN, 22)); display.setBackground(new Color(255, 255, 255));
display.setColumns(10); display.setEditable(false);
JPanel buttonPanel = new JPanel(); display.setFont(new Font("Tahoma", Font.PLAIN, 22));
GroupLayout gl_contentPane = new GroupLayout(contentPane); display.setColumns(10);
gl_contentPane.setHorizontalGroup( JPanel buttonPanel = new JPanel();
gl_contentPane.createParallelGroup(Alignment.LEADING) GroupLayout gl_contentPane = new GroupLayout(contentPane);
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup() gl_contentPane.setHorizontalGroup(
.addContainerGap() gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addComponent(buttonPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE) .addContainerGap()
.addComponent(display, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addContainerGap()) .addComponent(buttonPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)
); .addComponent(display, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
gl_contentPane.setVerticalGroup( .addContainerGap())
gl_contentPane.createParallelGroup(Alignment.LEADING) );
.addGroup(gl_contentPane.createSequentialGroup() gl_contentPane.setVerticalGroup(
.addGap(23) gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(display, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE) .addGroup(gl_contentPane.createSequentialGroup()
.addGap(18) .addGap(23)
.addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) .addComponent(display, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
.addContainerGap()) .addGap(18)
); .addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
buttonPanel.setLayout(new GridLayout(0, 5, 0, 0)); .addContainerGap())
);
JButton btn7 = new JButton("7"); buttonPanel.setLayout(new GridLayout(0, 5, 0, 0));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { JButton btn0 = new JButton("0");
processDigit(e.getActionCommand()); JButton btn1 = new JButton("1");
} JButton btn2 = new JButton("2");
}); JButton btn3 = new JButton("3");
buttonPanel.add(btn7); JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn8 = new JButton("8"); JButton btn6 = new JButton("6"); // why was 6 afraid of 7... because 7 8 9 :sob:
btn8.addActionListener(new ActionListener() { JButton btn7 = new JButton("7"); // 67!
public void actionPerformed(ActionEvent e) { JButton btn8 = new JButton("8");
processDigit(e.getActionCommand()); JButton btn9 = new JButton("9");
}
}); btn0.setFont(new Font("Tahoma", Font.PLAIN, 29));
buttonPanel.add(btn8); btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btn9 = new JButton("9"); processDigit(e.getActionCommand());
btn9.addActionListener(new ActionListener() { }
public void actionPerformed(ActionEvent e) { });
processDigit(e.getActionCommand());
} btn1.setFont(new Font("Tahoma", Font.PLAIN, 29));
}); btn1.addActionListener(new ActionListener() {
buttonPanel.add(btn9); public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
JButton btnDiv = new JButton("/"); }
btnDiv.addActionListener(new ActionListener() { });
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand()); btn2.setFont(new Font("Tahoma", Font.PLAIN, 29));
} btn2.addActionListener(new ActionListener() {
}); public void actionPerformed(ActionEvent e) {
buttonPanel.add(btnDiv); processDigit(e.getActionCommand());
}
JButton btnErase = new JButton("DEL"); });
btnErase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { btn3.setFont(new Font("Tahoma", Font.PLAIN, 29));
clear(); btn3.addActionListener(new ActionListener() {
} public void actionPerformed(ActionEvent e) {
}); processDigit(e.getActionCommand());
buttonPanel.add(btnErase); }
});
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() { btn4.setFont(new Font("Tahoma", Font.PLAIN, 29));
public void actionPerformed(ActionEvent e) { btn4.addActionListener(new ActionListener() {
processDigit(e.getActionCommand()); public void actionPerformed(ActionEvent e) {
} processDigit(e.getActionCommand());
}); }
buttonPanel.add(btn4); });
JButton btn5 = new JButton("5"); btn5.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn5.addActionListener(new ActionListener() { btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand()); processDigit(e.getActionCommand());
} }
}); });
buttonPanel.add(btn5);
btn6.setFont(new Font("Tahoma", Font.PLAIN, 29));
JButton btn6 = new JButton("6"); btn6.addActionListener(new ActionListener() {
btn6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) { processDigit(e.getActionCommand());
processDigit(e.getActionCommand()); }
} });
});
buttonPanel.add(btn6); btn7.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn7.addActionListener(new ActionListener() {
JButton btn3 = new JButton("3"); public void actionPerformed(ActionEvent e) {
btn3.addActionListener(new ActionListener() { processDigit(e.getActionCommand());
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) {
JButton btn1 = new JButton("1"); processDigit(e.getActionCommand());
btn1.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) {
JButton btnMul = new JButton("*"); processDigit(e.getActionCommand());
btnMul.addActionListener(new ActionListener() { }
public void actionPerformed(ActionEvent e) { });
setOperator(e.getActionCommand());
} // other stuff
}); JButton btnErase = new JButton("DEL");
buttonPanel.add(btnMul); JButton empty = new JButton("");
JButton btnClear = new JButton("C");
JButton btnClear = new JButton("C"); JButton btnPlusMin = new JButton("+/-");
btnClear.addActionListener(new ActionListener() { JButton btnDot = new JButton(".");
public void actionPerformed(ActionEvent e) {
clear(); btnErase.setFont(new Font("Tahoma", Font.PLAIN, 29));
} btnErase.addActionListener(new ActionListener() {
}); public void actionPerformed(ActionEvent e) {
buttonPanel.add(btnClear); clear();
buttonPanel.add(btn1); }
});
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() { empty.setBackground(Color.WHITE);
public void actionPerformed(ActionEvent e) { empty.setEnabled(false);
processDigit(e.getActionCommand());
} btnClear.setFont(new Font("Tahoma", Font.PLAIN, 29));
}); btnClear.addActionListener(new ActionListener() {
buttonPanel.add(btn2); public void actionPerformed(ActionEvent e) {
buttonPanel.add(btn3); clear();
}
});
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() { btnPlusMin.setFont(new Font("Tahoma", Font.PLAIN, 29));
public void actionPerformed(ActionEvent e) { btnPlusMin.addActionListener(new ActionListener() {
processDigit(e.getActionCommand()); public void actionPerformed(ActionEvent e) {
} String tmp = display.getText();
}); if (tmp == null || tmp.isEmpty()) return;
char first = tmp.charAt(0);
JButton btnPlus = new JButton("+"); if (first == '-') {
btnPlus.addActionListener(new ActionListener() { tmp = tmp.substring(1, tmp.length());
public void actionPerformed(ActionEvent e) { display.setText(tmp);
setOperator(e.getActionCommand()); } else {
} display.setText("-" + tmp);
}); }
buttonPanel.add(btnPlus); }
});
JButton empty = new JButton("");
empty.setBackground(Color.WHITE); btnDot.setFont(new Font("Tahoma", Font.PLAIN, 29));
empty.setEnabled(false); btnDot.addActionListener(new ActionListener() {
buttonPanel.add(empty); public void actionPerformed(ActionEvent e) {
String n = display.getText(); // get the text on the display
JButton btnPlusMin = new JButton("+/-"); if (n.contains(".")) return; // if it already contains a "." skip rest of this
btnPlusMin.addActionListener(new ActionListener() { processDigit(e.getActionCommand());
public void actionPerformed(ActionEvent e) {
String tmp = display.getText(); }
if (tmp == null || tmp.isEmpty()) return; });
char first = tmp.charAt(0);
if (first == '-') { // math stuff
tmp = tmp.substring(1, tmp.length());
display.setText(tmp); JButton btnDiv = new JButton("/");
} else { JButton btnMul = new JButton("*");
display.setText("-" + tmp); JButton btnMin = new JButton("-");
} JButton btnPlus = new JButton("+");
}
}); btnDiv.setFont(new Font("Tahoma", Font.PLAIN, 29));
buttonPanel.add(btnPlusMin); btnDiv.addActionListener(new ActionListener() {
buttonPanel.add(btn0); public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
JButton btnDot = new JButton("."); btnDiv.setForeground(new Color(30, 144, 255));
btnDot.addActionListener(new ActionListener() { btnMul.setForeground(new Color(0, 0, 0));
public void actionPerformed(ActionEvent e) { btnPlus.setForeground(new Color(0, 0, 0));
String n = display.getText(); // get the text on the display btnMin.setForeground(new Color(0, 0, 0));
if (n.contains(".")) return; // if it already contains a "." skip rest of this }
processDigit(e.getActionCommand()); });
} btnMul.setFont(new Font("Tahoma", Font.PLAIN, 29));
}); btnMul.addActionListener(new ActionListener() {
buttonPanel.add(btnDot); public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
JButton btnMin = new JButton("-"); btnDiv.setForeground(new Color(0, 0, 0));
btnMin.addActionListener(new ActionListener() { btnMul.setForeground(new Color(30, 144, 255));
public void actionPerformed(ActionEvent e) { btnPlus.setForeground(new Color(0, 0, 0));
setOperator(e.getActionCommand()); btnMin.setForeground(new Color(0, 0, 0));
} }
}); });
buttonPanel.add(btnMin);
btnMin.setFont(new Font("Tahoma", Font.PLAIN, 29));
JButton btnEq = new JButton("="); btnMin.addActionListener(new ActionListener() {
btnEq.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) { setOperator(e.getActionCommand());
operand2 = display.getText(); btnDiv.setForeground(new Color(0, 0, 0));
double o1 = Double.parseDouble(operand1); btnMul.setForeground(new Color(0, 0, 0));
double o2 = Double.parseDouble(operand2); btnPlus.setForeground(new Color(0, 0, 0));
btnMin.setForeground(new Color(30, 144, 255));
if (operator == "+") { }
result = o1+o2; });
} else if (operator == "-") {
result = o1-o2; btnPlus.setFont(new Font("Tahoma", Font.PLAIN, 29));
} else if (operator == "*") { btnPlus.addActionListener(new ActionListener() {
result = o1*o2; public void actionPerformed(ActionEvent e) {
} else if (operator == "/") { setOperator(e.getActionCommand());
result = o1/o2; btnDiv.setForeground(new Color(0, 0, 0));
} btnMul.setForeground(new Color(0, 0, 0));
operator = ""; btnPlus.setForeground(new Color(30, 144, 255));
operand1 = ""; btnMin.setForeground(new Color(0, 0, 0));
operand2 = ""; }
resultVisible = true; });
display.setText(""+result);
} JButton btnEq = new JButton("=");
}); btnEq.setFont(new Font("Tahoma", Font.PLAIN, 29));
buttonPanel.add(btnEq); btnEq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
contentPane.setLayout(gl_contentPane); operand2 = display.getText();
double o1 = Double.parseDouble(operand1);
} double o2 = Double.parseDouble(operand2);
protected void clear() { if (operator == "+") {
String tmp = display.getText(); result = o1+o2;
if (tmp.length()>1) // if there is more than 1 character } else if (operator == "-") {
display.setText( tmp.substring(0, tmp.length()-1) ); result = o1-o2;
else // otherwise just make display empty } else if (operator == "*") {
display.setText(""); result = o1*o2;
} } else if (operator == "/") {
result = o1/o2;
protected void processDigit(String actionCommand) { }
if (resultVisible == true) { operator = "";
display.setText(""); operand1 = "";
resultVisible = false; operand2 = "";
}
display.setText(display.getText() + actionCommand); btnDiv.setForeground(new Color(0, 0, 0));
btnMul.setForeground(new Color(0, 0, 0));
} btnPlus.setForeground(new Color(0, 0, 0));
protected void setOperator(String actionCommand) { btnMin.setForeground(new Color(0, 0, 0));
operand1 = display.getText();
operator = actionCommand; resultVisible = true;
display.setText(""); 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);
contentPane.setLayout(gl_contentPane);
}
protected void clear() {
String tmp = display.getText();
if (tmp.length()>1) // if there is more than 1 character
display.setText( tmp.substring(0, tmp.length()-1) );
else // otherwise just make display empty
display.setText("");
}
protected void processDigit(String actionCommand) {
if (resultVisible == true) {
display.setText("");
resultVisible = false;
}
display.setText(display.getText() + actionCommand);
}
protected void setOperator(String actionCommand) {
operand1 = display.getText();
operator = actionCommand;
display.setText("");
}
}