Created
April 15, 2024 05:54
-
-
Save kishida/21234e4f3a4d5f2947807aad6c46af57 to your computer and use it in GitHub Desktop.
calculator generated by CodeGemma 7B
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class Calculator extends JFrame implements ActionListener { | |
private JTextField textField; | |
private JButton[] numberButtons; | |
private JButton[] operatorButtons; | |
private JButton clearButton; | |
private JButton equalsButton; | |
private double firstNumber; | |
private double secondNumber; | |
private String operator; | |
public Calculator() { | |
// Set up the frame | |
setTitle("Calculator"); | |
setSize(400, 400); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// Create the text field | |
textField = new JTextField(); | |
textField.setFont(new Font("Arial", Font.BOLD, 20)); | |
textField.setHorizontalAlignment(JTextField.RIGHT); | |
add(textField, BorderLayout.NORTH); | |
// Create the number buttons | |
numberButtons = new JButton[10]; | |
for (int i = 0; i < 10; i++) { | |
numberButtons[i] = new JButton(String.valueOf(i)); | |
numberButtons[i].addActionListener(this); | |
} | |
// Create the operator buttons | |
operatorButtons = new JButton[] { | |
new JButton("+"), | |
new JButton("-"), | |
new JButton("*"), | |
new JButton("/") | |
}; | |
for (JButton button : operatorButtons) { | |
button.addActionListener(this); | |
} | |
// Create the clear button | |
clearButton = new JButton("C"); | |
clearButton.addActionListener(this); | |
// Create the equals button | |
equalsButton = new JButton("="); | |
equalsButton.addActionListener(this); | |
// Add the buttons to the frame | |
JPanel buttonPanel = new JPanel(); | |
buttonPanel.setLayout(new GridLayout(4, 4)); | |
for (int i = 0; i < 10; i++) { | |
buttonPanel.add(numberButtons[i]); | |
} | |
for (JButton button : operatorButtons) { | |
buttonPanel.add(button); | |
} | |
buttonPanel.add(clearButton); | |
buttonPanel.add(equalsButton); | |
add(buttonPanel, BorderLayout.CENTER); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String command = e.getActionCommand(); | |
// Handle number button clicks | |
if (command.matches("\\d")) { | |
textField.setText(textField.getText() + command); | |
} | |
// Handle operator button clicks | |
if (command.matches("[+\\-*/]")) { | |
firstNumber = Double.parseDouble(textField.getText()); | |
operator = command; | |
textField.setText(""); | |
} | |
// Handle clear button click | |
if (command.equals("C")) { | |
textField.setText(""); | |
firstNumber = 0; | |
secondNumber = 0; | |
operator = null; | |
} | |
// Handle equals button click | |
if (command.equals("=")) { | |
secondNumber = Double.parseDouble(textField.getText()); | |
double result = 0; | |
switch (operator) { | |
case "+": | |
result = firstNumber + secondNumber; | |
break; | |
case "-": | |
result = firstNumber - secondNumber; | |
break; | |
case "*": | |
result = firstNumber * secondNumber; | |
break; | |
case "/": | |
result = firstNumber / secondNumber; | |
break; | |
} | |
textField.setText(String.valueOf(result)); | |
} | |
} | |
public static void main(String[] args) { | |
new Calculator().setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prompt is "generate a calculator in Java swing"
https://gist.github.com/assets/1178746/42461b77-3e03-421b-9b24-aff24cfbf937