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