update to the new GUI version along with the addition to the new icons

This commit is contained in:
2025-12-12 09:24:34 -06:00
parent eee5475548
commit 2474fc05b1

View File

@@ -9,17 +9,23 @@ import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.util.ArrayList;
import java.util.List;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
public class Calculator extends JFrame implements KeyListener {
@@ -35,13 +41,11 @@ public class Calculator extends JFrame implements KeyListener {
private JButton btnPlus;
protected boolean resultVisible;
protected double result;
/**
* Launch the application.
*/
public static void main(String[] args) {
FlatMacDarkLaf.setup();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("com.formdev.flatlaf.themes.FlatMacDarkLaf");
} catch (Throwable e) {
e.printStackTrace();
}
@@ -57,61 +61,94 @@ public class Calculator extends JFrame implements KeyListener {
});
}
/**
* Create the frame.
*/
public Calculator() {
setBackground(new Color(32, 32, 32));
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);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
addKeyListener(this);
// setIconImage(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon.png")));
List<Image> icons = new ArrayList<>();
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon16.png")));
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon32.png")));
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon48.png")));
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon256.png")));
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon512.png")));
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon1024.png")));
setIconImages(icons);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
setJMenuBar(menuBar);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setTitle("angel's awesome calculator)");
setBounds(100, 100, 368, 556);
setFocusable(true);
addKeyListener(this);
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.setFont(new Font("Segoe UI", Font.PLAIN, 22));
display.setColumns(10);
JPanel buttonPanel = new JPanel();
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Dimension size = e.getComponent().getSize();
System.out.println("Window resized to " + size.width + "x" + size.height);
}
});
JPanel panel = 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.createSequentialGroup()
.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))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(display, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
.addComponent(buttonPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(5)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 379, GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(23)
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(display, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
.addContainerGap())
.addGap(27)
.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, 359, GroupLayout.PREFERRED_SIZE)
.addContainerGap(199, Short.MAX_VALUE))
);
buttonPanel.setLayout(new GridLayout(0, 5, 0, 0));
buttonPanel.setLayout(new GridLayout(0, 4, 0, 0));
JButton btnNewButton = new JButton("New button");
JButton btnNewButton_1 = new JButton("New button");
JLabel lblNewLabel = new JLabel("New label");
panel.add(btnNewButton);
panel.add(lblNewLabel);
panel.add(btnNewButton_1);
JButton empty_4 = new JButton("");
JButton empty_1 = new JButton("");
JButton empty_2 = new JButton("");
JButton empty = new JButton("");
JButton empty_3 = new JButton("");
JButton btnErase = new JButton("DEL");
JButton btnClear = new JButton("C");
JButton btnPlusMin = new JButton("+/-");
JButton btnDot = new JButton(".");
JButton btnEq = new JButton("=");
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
@@ -123,101 +160,121 @@ public class Calculator extends JFrame implements KeyListener {
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() {
btnDiv = new JButton("/");
btnMul = new JButton("*");
btnMin = new JButton("-");
btnPlus = 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());
}
});
btn8.setFont(new Font("Tahoma", Font.PLAIN, 29));
btn8.addActionListener(new ActionListener() {
btnEq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
math();
}
});
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();
}
});
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
btnPlusMin.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
btnMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processDigit(e.getActionCommand());
}
});
btnPlusMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tmp = display.getText();
@@ -231,88 +288,65 @@ public class Calculator extends JFrame implements KeyListener {
}
}
});
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
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());
}
});
btnErase.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnClear.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnPlusMin.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnEq.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnDot.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn0.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn1.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn2.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn3.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn4.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn5.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn6.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn7.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn8.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btn9.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnMul.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnMin.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnDiv.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnPlus.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnMul.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
empty.setEnabled(false);
empty_1.setEnabled(false);
empty_2.setEnabled(false);
empty_3.setEnabled(false);
empty_4.setEnabled(false);
btnMin.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
btnPlus.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setOperator(e.getActionCommand());
}
});
JButton btnEq = new JButton("=");
btnEq.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnEq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
math();
}
});
buttonPanel.add(empty);
buttonPanel.add(empty_3);
buttonPanel.add(btnClear);
buttonPanel.add(btnErase);
buttonPanel.add(empty_4);
buttonPanel.add(empty_1);
buttonPanel.add(empty_2);
buttonPanel.add(btnDiv);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btnDiv);
buttonPanel.add(btnErase);
buttonPanel.add(btnMul);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btnMul);
buttonPanel.add(btnClear);
buttonPanel.add(btnMin);
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() {
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) );
@@ -320,42 +354,42 @@ public class Calculator extends JFrame implements KeyListener {
display.setText("");
}
protected void processDigit(String actionCommand) {
void processDigit(String actionCommand) {
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));
}
// 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 daop) {
void setOperator(String daop) {
operand1 = display.getText();
operator = daop;
display.setText("");
}
protected void math() {
void math() {
operand2 = display.getText();
double op1 = Double.parseDouble(operand1);
double op2 = Double.parseDouble(operand2);
@@ -378,10 +412,10 @@ public class Calculator extends JFrame implements KeyListener {
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));
// 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);
@@ -429,4 +463,4 @@ public class Calculator extends JFrame implements KeyListener {
}
}
}
}