This repository has been archived by the owner on Oct 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from Mohit17067/master
Add a calculator using swing API - java.
- Loading branch information
Showing
16 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
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 not shown.
This file contains 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
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 not shown.
Binary file not shown.
This file contains 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
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.
This file contains 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
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); | ||
} | ||
} |