diff --git a/GUI_calculator/mohit17067/README.md b/GUI_calculator/mohit17067/README.md new file mode 100644 index 0000000..eb08d1d --- /dev/null +++ b/GUI_calculator/mohit17067/README.md @@ -0,0 +1,8 @@ +# Calc +A simple calculator app in Java, for CSE201 refresher module. + +### Instructions to run: +* Simply 'Run' if using Eclipse. You can change the main class in the run configuration. +* Compile using `javac *.java` from terminal, and run using `java LoginFrame`. + +**Update**: I have added a login page as an example of how to use JTextField effectively. diff --git a/GUI_calculator/mohit17067/bin/CalcFrame.class b/GUI_calculator/mohit17067/bin/CalcFrame.class new file mode 100644 index 0000000..2a2fc81 Binary files /dev/null and b/GUI_calculator/mohit17067/bin/CalcFrame.class differ diff --git a/GUI_calculator/mohit17067/bin/NumberPanel$1.class b/GUI_calculator/mohit17067/bin/NumberPanel$1.class new file mode 100644 index 0000000..dbc240e Binary files /dev/null and b/GUI_calculator/mohit17067/bin/NumberPanel$1.class differ diff --git a/GUI_calculator/mohit17067/bin/NumberPanel.class b/GUI_calculator/mohit17067/bin/NumberPanel.class new file mode 100644 index 0000000..8aa51ee Binary files /dev/null and b/GUI_calculator/mohit17067/bin/NumberPanel.class differ diff --git a/GUI_calculator/mohit17067/bin/OperationPanel$1.class b/GUI_calculator/mohit17067/bin/OperationPanel$1.class new file mode 100644 index 0000000..1568ecd Binary files /dev/null and b/GUI_calculator/mohit17067/bin/OperationPanel$1.class differ diff --git a/GUI_calculator/mohit17067/bin/OperationPanel.class b/GUI_calculator/mohit17067/bin/OperationPanel.class new file mode 100644 index 0000000..a415660 Binary files /dev/null and b/GUI_calculator/mohit17067/bin/OperationPanel.class differ diff --git a/GUI_calculator/mohit17067/src/CalcFrame.class b/GUI_calculator/mohit17067/src/CalcFrame.class new file mode 100644 index 0000000..352ad75 Binary files /dev/null and b/GUI_calculator/mohit17067/src/CalcFrame.class differ diff --git a/GUI_calculator/mohit17067/src/CalcFrame.java b/GUI_calculator/mohit17067/src/CalcFrame.java new file mode 100644 index 0000000..feb5525 --- /dev/null +++ b/GUI_calculator/mohit17067/src/CalcFrame.java @@ -0,0 +1,68 @@ +import java.awt.Color; +import java.awt.GridLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +public class CalcFrame extends JFrame { + + private JButton resultArea; + + public CalcFrame() { + setSize(300, 400); + setDefaultCloseOperation (EXIT_ON_CLOSE); + setLayout (new GridLayout(3, 1)); + + resultArea = new JButton (""); + resultArea.setEnabled(false); + resultArea.setBackground(Color.WHITE); + + add (resultArea); + resultArea.setBorder(new EmptyBorder(10, 10, 10, 10)); + + JPanel numPanel = new NumberPanel(this); + numPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); + add (numPanel); + + JPanel opPanel = new OperationPanel(this); + opPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); + add (opPanel); + + setVisible(true); + } + + public void writeDisplay (String expression) { + resultArea.setText(expression); + } + + public String readDisplay () { + return resultArea.getText(); + } + + public void evaluateExpression () { + String expression = readDisplay(); + double result = 0; + char operator = expression.charAt(1); + int operand1 = expression.charAt(0) - '0', operand2 = expression.charAt(2) - '0'; + if (operator == '+') { + result = operand1 + operand2; + } + else if (operator == '-') { + result = operand1 - operand2; + } + else if (operator == 'x') { + result = operand1 * operand2; + } + else if (operator == '/') { + result = operand1 / operand2; + } + + writeDisplay(Double.toString(result)); + } + + //public static void main(String[] args) { + // JFrame calcFrame = new CalcFrame(); + //} +} diff --git a/GUI_calculator/mohit17067/src/LoginFrame.class b/GUI_calculator/mohit17067/src/LoginFrame.class new file mode 100644 index 0000000..d0ab283 Binary files /dev/null and b/GUI_calculator/mohit17067/src/LoginFrame.class differ diff --git a/GUI_calculator/mohit17067/src/LoginFrame.java b/GUI_calculator/mohit17067/src/LoginFrame.java new file mode 100644 index 0000000..4f23bf9 --- /dev/null +++ b/GUI_calculator/mohit17067/src/LoginFrame.java @@ -0,0 +1,64 @@ +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPasswordField; +import javax.swing.JTextField; +import javax.swing.border.EmptyBorder; + +public class LoginFrame extends JFrame implements ActionListener { + private JTextField useridField; + private JPasswordField passwordField; + JFrame nextFrame; + + public LoginFrame() { + setSize(300, 400); + setDefaultCloseOperation (EXIT_ON_CLOSE); + + setLayout (new GridLayout(5, 1, 10, 10)); + + JLabel useridLabel = new JLabel("User ID"); + useridLabel.setBorder(new EmptyBorder(10, 10, 10, 10)); + add (useridLabel); + + useridField = new JTextField(); + useridField.setEditable(true); + useridField.setBorder(new EmptyBorder(10, 10, 10, 10)); + add(useridField); + + JLabel passwordLabel = new JLabel("Password"); + passwordLabel.setBorder(new EmptyBorder(10, 10, 10, 10));; + add (passwordLabel); + + passwordField = new JPasswordField(20); + passwordField.setEditable(true); + passwordField.setBorder(new EmptyBorder(10, 10, 10, 10)); + add(passwordField); + + JButton loginButton = new JButton("Login"); + loginButton.addActionListener(this); + loginButton.setBorder(new EmptyBorder(10, 10, 10, 10)); + add(loginButton); + + setVisible(true); + } + + public void actionPerformed(ActionEvent e) { + if (checkCredentials()) { + nextFrame = new CalcFrame(); + } + } + + private boolean checkCredentials() { + final String userid = useridField.getText().trim(); + final String password = passwordField.getText().trim(); + return (userid.equals("Ganesh") && password.equals("Gaitonde")); + } + + public static void main(String[] args) { + JFrame loginFrame = new LoginFrame(); + } +} diff --git a/GUI_calculator/mohit17067/src/NumberPanel$1.class b/GUI_calculator/mohit17067/src/NumberPanel$1.class new file mode 100644 index 0000000..31f5b68 Binary files /dev/null and b/GUI_calculator/mohit17067/src/NumberPanel$1.class differ diff --git a/GUI_calculator/mohit17067/src/NumberPanel.class b/GUI_calculator/mohit17067/src/NumberPanel.class new file mode 100644 index 0000000..22e993f Binary files /dev/null and b/GUI_calculator/mohit17067/src/NumberPanel.class differ diff --git a/GUI_calculator/mohit17067/src/NumberPanel.java b/GUI_calculator/mohit17067/src/NumberPanel.java new file mode 100644 index 0000000..0d1552b --- /dev/null +++ b/GUI_calculator/mohit17067/src/NumberPanel.java @@ -0,0 +1,33 @@ +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JPanel; + +public class NumberPanel extends JPanel { + private CalcFrame current_calc_state; + + public NumberPanel (CalcFrame c) { + current_calc_state = c; + + setLayout(new GridLayout(3, 3, 10, 10)); + + for (int i = 1; i < 10; i++) { + + final int num = i; + JButton NumButton = new JButton (Integer.toString (num)); + + NumButton.addActionListener (new ActionListener () { + @Override + public void actionPerformed (ActionEvent e) { + current_calc_state.writeDisplay (current_calc_state.readDisplay() + Integer.toString(num)); + } + }); + + add (NumButton); + } + + setVisible(true); + } +} diff --git a/GUI_calculator/mohit17067/src/OperationPanel$1.class b/GUI_calculator/mohit17067/src/OperationPanel$1.class new file mode 100644 index 0000000..74020f4 Binary files /dev/null and b/GUI_calculator/mohit17067/src/OperationPanel$1.class differ diff --git a/GUI_calculator/mohit17067/src/OperationPanel.class b/GUI_calculator/mohit17067/src/OperationPanel.class new file mode 100644 index 0000000..d8c5abf Binary files /dev/null and b/GUI_calculator/mohit17067/src/OperationPanel.class differ diff --git a/GUI_calculator/mohit17067/src/OperationPanel.java b/GUI_calculator/mohit17067/src/OperationPanel.java new file mode 100644 index 0000000..1446028 --- /dev/null +++ b/GUI_calculator/mohit17067/src/OperationPanel.java @@ -0,0 +1,44 @@ +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JPanel; + +public class OperationPanel extends JPanel { +private CalcFrame current_calc_state; + + private final String opers[] = {"+", "-", "x", "/", "=", "CLR"}; + + public OperationPanel (CalcFrame c) { + setLayout(new GridLayout(2, 3, 10, 10)); + + current_calc_state = c; + + for (int i = 0; i < opers.length; i++) { + + final String op = opers[i]; + JButton OpButton = new JButton (op); + + OpButton.addActionListener (new ActionListener () { + + @Override + public void actionPerformed (ActionEvent e) { + if (op == "=") { + current_calc_state.evaluateExpression(); + } + else if (op == "CLR"){ + current_calc_state.writeDisplay (""); + } + else { + current_calc_state.writeDisplay (current_calc_state.readDisplay() + op); + } + } + }); + + add (OpButton); + } + + setVisible(true); + } +}