Skip to content

Instantly share code, notes, and snippets.

@nivomi
Created May 30, 2012 00:27
Show Gist options
  • Save nivomi/2831671 to your computer and use it in GitHub Desktop.
Save nivomi/2831671 to your computer and use it in GitHub Desktop.
Really bad GUI calculator
switch(op){
case 1:
str = CalcGUI.getDisp().split(" \\+ ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x+y);
}catch(java.lang.NumberFormatException e){}
break;
case 2:
str = CalcGUI.getDisp().split(" – ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x-y);
}catch(java.lang.NumberFormatException e){}
break;
case 3:
str = CalcGUI.getDisp().split(" \\/ ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x / y);
}catch(java.lang.NumberFormatException e){}
break;
case 4:
str = CalcGUI.getDisp().split(" \\* ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x * y);
}catch(java.lang.NumberFormatException e){}
break;
default: System.out.println("Error 567: OPSwitch broken.");
break;
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CalcGUI extends JFrame {
JButton[] bnum = new MyButton[10];
JButton bp,bs;
JButton bClr,bPlus,bMinus,bDiv,bMulti,bEquals;
static JTextField equationSpace;
MyActionListener myAL = new MyActionListener();
public static void main (String args[]){
new CalcGUI();
}
CalcGUI() {
this.setSize(300,300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
//set up and add textbox
equationSpace = new JTextField("0",17);
equationSpace.setHorizontalAlignment(JTextField.RIGHT);
equationSpace.setFont(new Font("Arial", Font.PLAIN, 18));
JPanel outPanel = new JPanel();
JPanel operPanel = new JPanel();
outPanel.add(equationSpace);
content.add(outPanel, BorderLayout.NORTH);
//set up and add numpad
JPanel numPanel = new JPanel();
numPanel.setLayout(new GridLayout(4,3,3,3));
for(int i=0;i<10;i++)
bnum[i] = new MyButton("" + i);
numPanel.add(bnum[7]);
numPanel.add(bnum[8]);
numPanel.add(bnum[9]);
numPanel.add(bnum[4]);
numPanel.add(bnum[5]);
numPanel.add(bnum[6]);
numPanel.add(bnum[1]);
numPanel.add(bnum[2]);
numPanel.add(bnum[3]);
numPanel.add(bnum[0]);
JButton bp = new MyButton(".");
JButton bs = new MyButton("±");
numPanel.add(bp);
numPanel.add(bs);
content.add(numPanel, BorderLayout.WEST);
//set up and add opers
//bClr,bPlus,bMinus,bDiv,bMulti,bEquals
JPanel opPanel = new JPanel();
opPanel.setLayout(new GridLayout(6,1,0,3));
JButton bClr = new MyButton("Clr");
JButton bPlus = new MyButton("+");
JButton bMinus = new MyButton("-");
JButton bDiv = new MyButton("/");
JButton bMulti = new MyButton("*");
JButton bEquals = new MyButton("=");
opPanel.add(bPlus);
opPanel.add(bMinus);
opPanel.add(bMulti);
opPanel.add(bDiv);
opPanel.add(bEquals);
opPanel.add(bClr);
content.add(opPanel, BorderLayout.EAST);
this.validate();
this.setVisible(true);
}
private class MyButton extends JButton{ //code stolen!
MyButton(String s) {
super(s); //create the JButton just like normal
this.setBackground(new Color(230,240,255));
this.setFont(new Font("Arial", Font.PLAIN, 18));
this.addActionListener(myAL);
}
}
static void setDisp(String s){
equationSpace.setText(s);
}
static void setDisp(Double d){
equationSpace.setText(d.toString());
}
static String getDisp(){
return equationSpace.getText();
}
private class MyActionListener implements ActionListener {
public void actionPerformed( ActionEvent e ) {
String txt = e.getActionCommand();
CalcMain.buttonCatch(txt);
}
}
}
public class CalcMain {
static CalcGUI myCalc;
static int op = -1;
static boolean shouldClear = true;
static boolean addDecimal;
static boolean isOpSet = false;
public static void main(String args[]){
myCalc = new CalcGUI();
}
static void buttonCatch(String s){
//System.out.println(s);
if (s.hashCode() >= 48 && s.hashCode() <= 58 && shouldClear == true){
CalcGUI.setDisp("");
shouldClear = false;
}
//todo: code stuff
switch(s.hashCode()) { //so it turns out selectcase only supports ints. And so, hacky hashcode usage.
case 46: CalcGUI.setDisp(CalcGUI.getDisp() + ".");
break;
//the following four are
case 43: if (op != -1) break;
op = 1; //plus
CalcGUI.setDisp(CalcGUI.getDisp() + " + ");
isOpSet = true;
break;
case 45: if (op != -1) break;
op = 2; //minus
CalcGUI.setDisp(CalcGUI.getDisp() + " – ");
isOpSet = true;
break;
case 47: if (op != -1) break;
op = 3; //div
CalcGUI.setDisp(CalcGUI.getDisp() + " / ");
isOpSet = true;
break;
case 42: if (op != -1) break;
op = 4; //multi
CalcGUI.setDisp(CalcGUI.getDisp() + " * ");
isOpSet = true;
break;
case 177: //do ± stuff!
char c;
boolean foundNeg = false;
String[] str = new String[2];
for (int x = CalcGUI.getDisp().length() -1;x >= 0;x--){
c = CalcGUI.getDisp().charAt(x);
if (c == '-'){ //using the EN DASH (provided here: – for minuses. Enter... the unreadability zone!
str = CalcGUI.getDisp().split("\\-",2);
CalcGUI.setDisp(str[0] + str[1]);
foundNeg = true;
break;
}
else if(c == '+' || c == '–' || c=='*' || c=='/'){
switch(op){
case 1:
str = CalcGUI.getDisp().split(" \\+ ",2);
CalcGUI.setDisp(str[0] + " + -" + str[1]);
break;
case 2:
str = CalcGUI.getDisp().split(" – ",2);
CalcGUI.setDisp(str[0] + " – -" + str[1]);
break;
case 3:
str = CalcGUI.getDisp().split(" \\/ ",2);
CalcGUI.setDisp(str[0] + " / -" + str[1]);
break;
case 4:
str = CalcGUI.getDisp().split(" \\* ",2);
CalcGUI.setDisp(str[0] + " * -" + str[1]);
break;
}
foundNeg = true;
break;
}
}
if (foundNeg == false){
CalcGUI.setDisp("-" + CalcGUI.getDisp());
}
break;
case 67849: CalcGUI.setDisp(""); //clear button, clears the screen and stuff
isOpSet = false;
op = -1;
break;
case 61: if (isOpSet == false){ //Equals button, do the maaath
break;
}
double x,y;
isOpSet = false;
String[] nums = new String[2];
switch(op){
case 1:
str = CalcGUI.getDisp().split(" \\+ ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x+y);
}catch(java.lang.NumberFormatException e){}
break;
case 2:
str = CalcGUI.getDisp().split(" – ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x-y);
}catch(java.lang.NumberFormatException e){}
break;
case 3:
str = CalcGUI.getDisp().split(" \\/ ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x / y);
}catch(java.lang.NumberFormatException e){}
break;
case 4:
str = CalcGUI.getDisp().split(" \\* ",2);
try{
x = Double.parseDouble(str[0]);
y = Double.parseDouble(str[1]);
CalcGUI.setDisp(x * y);
}catch(java.lang.NumberFormatException e){}
break;
default: System.out.println("Error 567: OPSwitch broken.");
break;
}
op = -1;
//number cases
case 48:
CalcGUI.setDisp(CalcGUI.getDisp() + "0");
break;
case 49: CalcGUI.setDisp(CalcGUI.getDisp() + "1");
break;
case 50: CalcGUI.setDisp(CalcGUI.getDisp() + "2");
break;
case 51: CalcGUI.setDisp(CalcGUI.getDisp() + "3");
break;
case 52: CalcGUI.setDisp(CalcGUI.getDisp() + "4");
break;
case 53: CalcGUI.setDisp(CalcGUI.getDisp() + "5");
break;
case 54: CalcGUI.setDisp(CalcGUI.getDisp() + "6");
break;
case 55: CalcGUI.setDisp(CalcGUI.getDisp() + "7");
break;
case 56: CalcGUI.setDisp(CalcGUI.getDisp() + "8");
break;
case 57: CalcGUI.setDisp(CalcGUI.getDisp() + "9");
break;
default: System.out.println("Error 432: Non-button command received!");
break;
}
//System.out.println("Debugtime:\n Switch ID 1 complete!");
//.out.println("current status of op:" + op);
//System.out.println(s.hashCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment