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.GroupLayout.Alignment;
import javax.swing.JTextField; import javax.swing.JTextField;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.JButton; import javax.swing.JButton;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.Toolkit; import java.awt.Toolkit;
import javax.swing.JMenuBar; import java.util.ArrayList;
import javax.swing.JMenu; import java.util.List;
import javax.swing.JMenuItem; import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
public class Calculator extends JFrame implements KeyListener { public class Calculator extends JFrame implements KeyListener {
@@ -35,13 +41,11 @@ public class Calculator extends JFrame implements KeyListener {
private JButton btnPlus; private JButton btnPlus;
protected boolean resultVisible; protected boolean resultVisible;
protected double result; protected double result;
/**
* Launch the application.
*/
public static void main(String[] args) { public static void main(String[] args) {
FlatMacDarkLaf.setup();
try { try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); UIManager.setLookAndFeel("com.formdev.flatlaf.themes.FlatMacDarkLaf");
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -57,61 +61,94 @@ public class Calculator extends JFrame implements KeyListener {
}); });
} }
/**
* Create the frame.
*/
public Calculator() { public Calculator() {
setBackground(new Color(32, 32, 32));
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)"); List<Image> icons = new ArrayList<>();
setBounds(100, 100, 634, 553); icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon16.png")));
setFocusable(true); icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon32.png")));
setFocusTraversalKeysEnabled(false); icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon48.png")));
addKeyListener(this); 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(); setTitle("angel's awesome calculator)");
JMenu fileMenu = new JMenu("File"); setBounds(100, 100, 368, 556);
JMenuItem exitItem = new JMenuItem("Exit"); setFocusable(true);
addKeyListener(this);
exitItem.addActionListener(e -> System.exit(0));
setJMenuBar(menuBar);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
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.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("Segoe UI", Font.PLAIN, 22));
display.setColumns(10); display.setColumns(10);
JPanel buttonPanel = new JPanel(); 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); GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup( gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING) gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(buttonPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE) .addGroup(gl_contentPane.createSequentialGroup()
.addComponent(display, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)) .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()) .addContainerGap())
); );
gl_contentPane.setVerticalGroup( gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING) gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup() .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) .addComponent(display, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
.addGap(18) .addGap(27)
.addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) .addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, 359, GroupLayout.PREFERRED_SIZE)
.addContainerGap()) .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 btn0 = new JButton("0");
JButton btn1 = new JButton("1"); JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2"); JButton btn2 = new JButton("2");
@@ -123,101 +160,121 @@ public class Calculator extends JFrame implements KeyListener {
JButton btn8 = new JButton("8"); JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9"); JButton btn9 = new JButton("9");
btn0.setFont(new Font("Tahoma", Font.PLAIN, 29)); btnDiv = new JButton("/");
btn0.addActionListener(new ActionListener() { btnMul = new JButton("*");
public void actionPerformed(ActionEvent e) { btnMin = new JButton("-");
processDigit(e.getActionCommand()); btnPlus = new JButton("+");
}
}); btnDot.addActionListener(new ActionListener() {
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) { 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()); processDigit(e.getActionCommand());
} }
}); });
btn8.setFont(new Font("Tahoma", Font.PLAIN, 29)); btnEq.addActionListener(new ActionListener() {
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { 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() { btnErase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
clear(); clear();
} }
}); });
empty.setBackground(Color.WHITE);
empty.setEnabled(false);
btnClear.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnClear.addActionListener(new ActionListener() { btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
clear(); 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() { btnPlusMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String tmp = display.getText(); String tmp = display.getText();
@@ -231,88 +288,65 @@ public class Calculator extends JFrame implements KeyListener {
} }
} }
}); });
btnDot.setFont(new Font("Tahoma", Font.PLAIN, 29)); btnErase.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnDot.addActionListener(new ActionListener() { btnClear.setFont(new Font("Segoe UI", Font.PLAIN, 29));
public void actionPerformed(ActionEvent e) { btnPlusMin.setFont(new Font("Segoe UI", Font.PLAIN, 29));
String n = display.getText(); // get the text on the display btnEq.setFont(new Font("Segoe UI", Font.PLAIN, 29));
if (n.contains(".")) return; // if it already contains a "." skip rest of this btnDot.setFont(new Font("Segoe UI", Font.PLAIN, 29));
processDigit(e.getActionCommand()); 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));
// math stuff btn5.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnDiv = new JButton("/"); btn6.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnMul = new JButton("*"); btn7.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnMin = new JButton("-"); btn8.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnPlus = new JButton("+"); btn9.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnMul.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnDiv.setFont(new Font("Tahoma", Font.PLAIN, 29)); btnMin.setFont(new Font("Segoe UI", Font.PLAIN, 29));
btnDiv.addActionListener(new ActionListener() { btnDiv.setFont(new Font("Segoe UI", Font.PLAIN, 29));
public void actionPerformed(ActionEvent e) { btnPlus.setFont(new Font("Segoe UI", Font.PLAIN, 29));
setOperator(e.getActionCommand());
}
});
btnMul.setFont(new Font("Tahoma", Font.PLAIN, 29)); empty.setEnabled(false);
btnMul.addActionListener(new ActionListener() { empty_1.setEnabled(false);
public void actionPerformed(ActionEvent e) { empty_2.setEnabled(false);
setOperator(e.getActionCommand()); empty_3.setEnabled(false);
} empty_4.setEnabled(false);
});
btnMin.setFont(new Font("Tahoma", Font.PLAIN, 29)); buttonPanel.add(empty);
btnMin.addActionListener(new ActionListener() { buttonPanel.add(empty_3);
public void actionPerformed(ActionEvent e) { buttonPanel.add(btnClear);
setOperator(e.getActionCommand()); buttonPanel.add(btnErase);
} buttonPanel.add(empty_4);
}); buttonPanel.add(empty_1);
buttonPanel.add(empty_2);
btnPlus.setFont(new Font("Tahoma", Font.PLAIN, 29)); buttonPanel.add(btnDiv);
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(btn7); buttonPanel.add(btn7);
buttonPanel.add(btn8); buttonPanel.add(btn8);
buttonPanel.add(btn9); buttonPanel.add(btn9);
buttonPanel.add(btnDiv); buttonPanel.add(btnMul);
buttonPanel.add(btnErase);
buttonPanel.add(btn4); buttonPanel.add(btn4);
buttonPanel.add(btn5); buttonPanel.add(btn5);
buttonPanel.add(btn6); buttonPanel.add(btn6);
buttonPanel.add(btnMul); buttonPanel.add(btnMin);
buttonPanel.add(btnClear);
buttonPanel.add(btn1); buttonPanel.add(btn1);
buttonPanel.add(btn2); buttonPanel.add(btn2);
buttonPanel.add(btn3); buttonPanel.add(btn3);
buttonPanel.add(btnPlus); buttonPanel.add(btnPlus);
buttonPanel.add(empty);
buttonPanel.add(btnPlusMin); buttonPanel.add(btnPlusMin);
buttonPanel.add(btn0); buttonPanel.add(btn0);
buttonPanel.add(btnDot); buttonPanel.add(btnDot);
buttonPanel.add(btnMin);
buttonPanel.add(btnEq); buttonPanel.add(btnEq);
contentPane.setLayout(gl_contentPane); contentPane.setLayout(gl_contentPane);
} }
protected void clear() { void clear() {
String tmp = display.getText(); String tmp = display.getText();
if (tmp.length()>1) // if there is more than 1 character if (tmp.length()>1) // if there is more than 1 character
display.setText( tmp.substring(0, tmp.length()-1) ); display.setText( tmp.substring(0, tmp.length()-1) );
@@ -320,42 +354,42 @@ public class Calculator extends JFrame implements KeyListener {
display.setText(""); display.setText("");
} }
protected void processDigit(String actionCommand) { void processDigit(String actionCommand) {
if (resultVisible == true) { if (resultVisible == true) {
display.setText(""); display.setText("");
resultVisible = false; resultVisible = false;
} }
if (actionCommand == "+") { // if (actionCommand == "+") {
btnDiv.setForeground(new Color(0, 0, 0)); // btnDiv.setForeground(new Color(0, 0, 0));
btnMul.setForeground(new Color(0, 0, 0)); // btnMul.setForeground(new Color(0, 0, 0));
btnPlus.setForeground(new Color(30, 144, 255)); // btnPlus.setForeground(new Color(30, 144, 255));
btnMin.setForeground(new Color(0, 0, 0)); // btnMin.setForeground(new Color(0, 0, 0));
} else if (actionCommand == "*") { // } else if (actionCommand == "*") {
btnDiv.setForeground(new Color(0, 0, 0)); // btnDiv.setForeground(new Color(0, 0, 0));
btnMul.setForeground(new Color(30, 144, 255)); // btnMul.setForeground(new Color(30, 144, 255));
btnPlus.setForeground(new Color(0, 0, 0)); // btnPlus.setForeground(new Color(0, 0, 0));
btnMin.setForeground(new Color(0, 0, 0)); // btnMin.setForeground(new Color(0, 0, 0));
} else if (actionCommand == "/") { // } else if (actionCommand == "/") {
btnDiv.setForeground(new Color(30, 144, 255)); // btnDiv.setForeground(new Color(30, 144, 255));
btnMul.setForeground(new Color(0, 0, 0)); // btnMul.setForeground(new Color(0, 0, 0));
btnPlus.setForeground(new Color(0, 0, 0)); // btnPlus.setForeground(new Color(0, 0, 0));
btnMin.setForeground(new Color(0, 0, 0)); // btnMin.setForeground(new Color(0, 0, 0));
} else if (actionCommand == "-") { // } else if (actionCommand == "-") {
btnDiv.setForeground(new Color(0, 0, 0)); // btnDiv.setForeground(new Color(0, 0, 0));
btnMul.setForeground(new Color(0, 0, 0)); // btnMul.setForeground(new Color(0, 0, 0));
btnPlus.setForeground(new Color(0, 0, 0)); // btnPlus.setForeground(new Color(0, 0, 0));
btnMin.setForeground(new Color(30, 144, 255)); // btnMin.setForeground(new Color(30, 144, 255));
} // }
display.setText(display.getText() + actionCommand); display.setText(display.getText() + actionCommand);
} }
protected void setOperator(String daop) { void setOperator(String daop) {
operand1 = display.getText(); operand1 = display.getText();
operator = daop; operator = daop;
display.setText(""); display.setText("");
} }
protected void math() { void math() {
operand2 = display.getText(); operand2 = display.getText();
double op1 = Double.parseDouble(operand1); double op1 = Double.parseDouble(operand1);
double op2 = Double.parseDouble(operand2); double op2 = Double.parseDouble(operand2);
@@ -378,10 +412,10 @@ public class Calculator extends JFrame implements KeyListener {
operand1 = ""; operand1 = "";
operand2 = ""; operand2 = "";
btnDiv.setForeground(new Color(0, 0, 0)); // btnDiv.setForeground(new Color(0, 0, 0));
btnMul.setForeground(new Color(0, 0, 0)); // btnMul.setForeground(new Color(0, 0, 0));
btnPlus.setForeground(new Color(0, 0, 0)); // btnPlus.setForeground(new Color(0, 0, 0));
btnMin.setForeground(new Color(0, 0, 0)); // btnMin.setForeground(new Color(0, 0, 0));
resultVisible = true; resultVisible = true;
display.setText(""+result); display.setText(""+result);
@@ -429,4 +463,4 @@ public class Calculator extends JFrame implements KeyListener {
} }
} }
} }