Created
September 2, 2014 19:48
-
-
Save matutter/9aaa09cf3c269bf5ba2c to your computer and use it in GitHub Desktop.
java calc example: This is an example of java.swing.* usage. Non-GUI features are intentionally poorly implemented. Don't plagiarizer.
This file contains hidden or 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
| import java.awt.*; | |
| import javax.swing.*; | |
| import java.awt.event.*; | |
| class Calculator extends JFrame implements ActionListener { | |
| int cols = 6; | |
| JPanel[] row = new JPanel[cols]; | |
| JButton[] button = new JButton[20]; | |
| String[] buttonString = {"7", "8", "9", "+", | |
| "4", "5", "6", "-", | |
| "1", "2", "3", "*", | |
| ".", "/", "C", "", | |
| "", "=", "0", ""}; | |
| Dimension text_area_dim = new Dimension(300, 40); // text box | |
| Dimension normal_button_dim = new Dimension(40, 40); // regular column buttons | |
| boolean[] function = new boolean[4]; | |
| double[] temporary = {0, 0}; | |
| JTextArea display = new JTextArea(1,20); | |
| //JTextArea display2 = new JTextArea(1,20); | |
| Font font = new Font("Times new Roman", Font.BOLD, 14); | |
| Calculator() { | |
| //super("Calculator"); | |
| setDesign(); | |
| setSize(380, 250); | |
| setResizable(false); | |
| setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| GridLayout grid = new GridLayout(5,6); // rowsXcols | |
| setLayout(grid); | |
| for(int i = 0; i < cols; i++) | |
| row[i] = new JPanel(); //add jpanels to our jfram indieces | |
| for(int i = 0; i < 20; i++) { | |
| button[i] = new JButton(); | |
| button[i].setText(buttonString[i]); | |
| button[i].setFont(font); | |
| button[i].addActionListener(this); | |
| } | |
| display.setFont(font); | |
| display.setEditable(false); | |
| display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); | |
| display.setPreferredSize(text_area_dim); | |
| for(int i = 0; i < 20; i++) { | |
| button[i].setPreferredSize(normal_button_dim); | |
| if( buttonString[i] == "" ) { | |
| button[i].setOpaque(false); | |
| button[i].setContentAreaFilled(false); | |
| button[i].setBorderPainted(false); | |
| } | |
| } | |
| int thisRow = 0; | |
| row[thisRow].add(display); | |
| add(row[thisRow]); | |
| thisRow++; | |
| for(int i = 0; i < 4; i++) | |
| row[thisRow].add(button[i]); | |
| row[thisRow].add(button[14]); | |
| add(row[thisRow]); | |
| thisRow++; | |
| for(int i = 4; i < 8; i++) | |
| row[thisRow].add(button[i]); | |
| row[thisRow].add(button[15]); | |
| add(row[thisRow]); | |
| thisRow++; | |
| for(int i = 8; i < 12; i++) | |
| row[thisRow].add(button[i]); | |
| row[thisRow].add(button[16]); | |
| add(row[thisRow]); | |
| thisRow++; | |
| row[thisRow].add(button[18]); | |
| for(int i = 12; i < 14; i++) | |
| row[thisRow].add(button[i]); | |
| row[thisRow].add(button[17]); | |
| add(row[thisRow]); | |
| setVisible(true); | |
| } | |
| public void clear() { | |
| try { | |
| display.setText(""); | |
| for(int i = 0; i < 4; i++) | |
| function[i] = false; | |
| for(int i = 0; i < 2; i++) | |
| temporary[i] = 0; | |
| } catch(NullPointerException e) { | |
| } | |
| } | |
| public void getSqrt() { | |
| try { | |
| double value = Math.sqrt(Double.parseDouble(display.getText())); | |
| display.setText(Double.toString(value)); | |
| } catch(NumberFormatException e) { | |
| } | |
| } | |
| public void getPosNeg() { | |
| try { | |
| double value = Double.parseDouble(display.getText()); | |
| if(value != 0) { | |
| value = value * (-1); | |
| display.setText(Double.toString(value)); | |
| } | |
| else { | |
| } | |
| } catch(NumberFormatException e) { | |
| } | |
| } | |
| public void getResult() { | |
| double result = 0; | |
| temporary[1] = Double.parseDouble(display.getText()); | |
| String temp0 = Double.toString(temporary[0]); | |
| String temp1 = Double.toString(temporary[1]); | |
| try { | |
| if(temp0.contains("-")) { | |
| String[] temp00 = temp0.split("-", 2); | |
| temporary[0] = (Double.parseDouble(temp00[1]) * -1); | |
| } | |
| if(temp1.contains("-")) { | |
| String[] temp11 = temp1.split("-", 2); | |
| temporary[1] = (Double.parseDouble(temp11[1]) * -1); | |
| } | |
| } catch(ArrayIndexOutOfBoundsException e) { | |
| } | |
| try { | |
| if(function[2] == true) | |
| result = temporary[0] * temporary[1]; | |
| else if(function[3] == true) | |
| result = temporary[0] / temporary[1]; | |
| else if(function[0] == true) | |
| result = temporary[0] + temporary[1]; | |
| else if(function[1] == true) | |
| result = temporary[0] - temporary[1]; | |
| display.setText(Double.toString(result)); | |
| for(int i = 0; i < 4; i++) | |
| function[i] = false; | |
| } catch(NumberFormatException e) { | |
| } | |
| } | |
| public final void setDesign() { | |
| try { | |
| UIManager.setLookAndFeel( | |
| "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); | |
| } catch(Exception e) { | |
| } | |
| } | |
| @Override | |
| public void actionPerformed(ActionEvent ae) { | |
| if(ae.getSource() == button[0]) | |
| display.append("7"); | |
| if(ae.getSource() == button[1]) | |
| display.append("8"); | |
| if(ae.getSource() == button[2]) | |
| display.append("9"); | |
| if(ae.getSource() == button[3]) { | |
| //add function[0] | |
| temporary[0] = Double.parseDouble(display.getText()); | |
| function[0] = true; | |
| display.setText(""); | |
| } | |
| if(ae.getSource() == button[4]) | |
| display.append("4"); | |
| if(ae.getSource() == button[5]) | |
| display.append("5"); | |
| if(ae.getSource() == button[6]) | |
| display.append("6"); | |
| if(ae.getSource() == button[7]) { | |
| //subtract function[1] | |
| temporary[0] = Double.parseDouble(display.getText()); | |
| function[1] = true; | |
| display.setText(""); | |
| } | |
| if(ae.getSource() == button[8]) | |
| display.append("1"); | |
| if(ae.getSource() == button[9]) | |
| display.append("2"); | |
| if(ae.getSource() == button[10]) | |
| display.append("3"); | |
| if(ae.getSource() == button[11]) { | |
| //multiply function[2] | |
| temporary[0] = Double.parseDouble(display.getText()); | |
| function[2] = true; | |
| display.setText(""); | |
| } | |
| if(ae.getSource() == button[12]) | |
| display.append("."); | |
| if(ae.getSource() == button[13]) { | |
| //divide function[3] | |
| temporary[0] = Double.parseDouble(display.getText()); | |
| function[3] = true; | |
| display.setText(""); | |
| } | |
| if(ae.getSource() == button[14]) | |
| clear(); | |
| if(ae.getSource() == button[15]) | |
| getSqrt(); | |
| if(ae.getSource() == button[16]) | |
| getPosNeg(); | |
| if(ae.getSource() == button[17]) | |
| getResult(); | |
| if(ae.getSource() == button[18]) | |
| display.append("0"); | |
| } | |
| public static void main(String[] arguments) { | |
| Calculator c = new Calculator(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment