main
parent
30ae920f03
commit
4b5738a3ac
@ -0,0 +1,48 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class SimpleCalculator extends JFrame {
|
||||||
|
|
||||||
|
private JTextField display;
|
||||||
|
private JPanel panel;
|
||||||
|
private double operand1 = 0;
|
||||||
|
private double operand2 = 0;
|
||||||
|
private char operator;
|
||||||
|
private boolean userIsTypingSecondNumber = false;
|
||||||
|
|
||||||
|
public SimpleCalculator() {
|
||||||
|
setTitle("Simple Calculator");
|
||||||
|
setSize(400, 500);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// Display text field
|
||||||
|
display = new JTextField();
|
||||||
|
display.setEditable(false);
|
||||||
|
display.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||||
|
add(display, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
// Panel for buttons
|
||||||
|
panel = new JPanel();
|
||||||
|
panel.setLayout(new GridLayout(5, 4, 10, 10));
|
||||||
|
|
||||||
|
// Add numbers and operations to panel
|
||||||
|
String[] buttons = {
|
||||||
|
"7", "8", "9", "/",
|
||||||
|
"4", "5", "6", "*",
|
||||||
|
"1", "2", "3", "-",
|
||||||
|
"0", ".", "=", "+",
|
||||||
|
"C"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String text : buttons) {
|
||||||
|
JButton button = new JButton(text);
|
||||||
|
button.setFont(new Font("Verdana", Font.PLAIN, 24));
|
||||||
|
button.setFocusable(false);
|
||||||
|
panel.add(button);
|
||||||
|
button.addActionListener(new ButtonClickListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
add(panel, BorderLayout.CENTER);
|
||||||
|
}
|
Loading…
Reference in new issue