Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Add a calculator using swing API - java.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohit17067 committed Oct 8, 2018
1 parent 1213170 commit ef23907
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 0 deletions.
8 changes: 8 additions & 0 deletions GUI_calculator/mohit17067/README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added GUI_calculator/mohit17067/bin/CalcFrame.class
Binary file not shown.
Binary file not shown.
Binary file added GUI_calculator/mohit17067/bin/NumberPanel.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added GUI_calculator/mohit17067/src/CalcFrame.class
Binary file not shown.
68 changes: 68 additions & 0 deletions GUI_calculator/mohit17067/src/CalcFrame.java
Original file line number Diff line number Diff line change
@@ -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();
//}
}
Binary file added GUI_calculator/mohit17067/src/LoginFrame.class
Binary file not shown.
64 changes: 64 additions & 0 deletions GUI_calculator/mohit17067/src/LoginFrame.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Binary file added GUI_calculator/mohit17067/src/NumberPanel$1.class
Binary file not shown.
Binary file added GUI_calculator/mohit17067/src/NumberPanel.class
Binary file not shown.
33 changes: 33 additions & 0 deletions GUI_calculator/mohit17067/src/NumberPanel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Binary file not shown.
Binary file not shown.
44 changes: 44 additions & 0 deletions GUI_calculator/mohit17067/src/OperationPanel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ef23907

Please sign in to comment.