Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package GUI;
- import java.util.Scanner;
- /**
- *
- * @author Caspar Cedro
- */
- public class CalculatorGUI extends javax.swing.JFrame {
- Integer firstDouble = null;
- Integer secondDouble = null;
- public void operation (String operator){
- double resultant = firstDouble + operator + secondDouble ;
- }
- //Integer operator = null;
- /*
- //Variables
- double plusminus;
- double firstDouble;
- double secondDouble;
- double totalDouble;
- //To check for button click
- int plusClick;
- int minusClick;
- int multiplyClick;
- int divideClick;
- */
- /**
- * Creates new form CalculatorGUI
- */
- public CalculatorGUI() {
- initComponents();
- //Insert code to operate calculator display here!
- //result.setText("test");
- //result.setText("");
- //Scanner input = new Scanner(System.in);
- result.setText("");
- //int answer = input.nextInt();
- /*if (input.hasNextInt()){
- result.setText("("+input+")");
- /*if (answer >= 0){
- result.setText("("+answer+")");
- } else {
- result.setText("");
- }*/
- //result.setText("("+answer+")");
- /*if (input.hasNextInt()) {
- result.setText("(" + input.nextInt() + ")");
- } else {
- result.setText("");
- }
- } else {
- result.setText("");
- }*/
- }
- /**
- * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- division = new javax.swing.JButton();
- one = new javax.swing.JButton();
- four = new javax.swing.JButton();
- seven = new javax.swing.JButton();
- multiplication = new javax.swing.JButton();
- two = new javax.swing.JButton();
- five = new javax.swing.JButton();
- eight = new javax.swing.JButton();
- addition = new javax.swing.JButton();
- three = new javax.swing.JButton();
- six = new javax.swing.JButton();
- nine = new javax.swing.JButton();
- subtraction = new javax.swing.JButton();
- zero = new javax.swing.JButton();
- equals = new javax.swing.JButton();
- decimal = new javax.swing.JButton();
- clear = new javax.swing.JButton();
- clearAll = new javax.swing.JButton();
- result = new javax.swing.JFormattedTextField();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- division.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- division.setText("รท");
- division.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- division.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- divisionActionPerformed(evt);
- }
- });
- one.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- one.setText("1");
- one.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- oneActionPerformed(evt);
- }
- });
- four.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- four.setText("4");
- four.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- fourActionPerformed(evt);
- }
- });
- seven.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- seven.setText("7");
- seven.setToolTipText("");
- seven.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- sevenActionPerformed(evt);
- }
- });
- multiplication.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- multiplication.setText("x");
- multiplication.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- multiplicationActionPerformed(evt);
- }
- });
- two.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- two.setText("2");
- two.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- twoActionPerformed(evt);
- }
- });
- five.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- five.setText("5");
- five.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- fiveActionPerformed(evt);
- }
- });
- eight.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- eight.setText("8");
- eight.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- eightActionPerformed(evt);
- }
- });
- addition.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- addition.setText("+");
- addition.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- addition.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- additionActionPerformed(evt);
- }
- });
- three.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- three.setText("3");
- three.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- threeActionPerformed(evt);
- }
- });
- six.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- six.setText("6");
- six.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- six.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- sixActionPerformed(evt);
- }
- });
- nine.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- nine.setText("9");
- nine.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- nineActionPerformed(evt);
- }
- });
- subtraction.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
- subtraction.setText("-");
- subtraction.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- subtraction.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- subtractionActionPerformed(evt);
- }
- });
- zero.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- zero.setText("0");
- zero.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- zeroActionPerformed(evt);
- }
- });
- equals.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- equals.setText("=");
- equals.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- equalsActionPerformed(evt);
- }
- });
- decimal.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- decimal.setText(".");
- clear.setText("C");
- clearAll.setText("CA");
- clearAll.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- clearAllActionPerformed(evt);
- }
- });
- result.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- resultActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(result)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(one, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(four, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(seven, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(division))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(two, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(five, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(eight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(multiplication, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(zero)))
- .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(decimal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(addition, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(subtraction, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
- .addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addComponent(clearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(0, 0, Short.MAX_VALUE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(clearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(8, 8, 8)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(division, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(addition, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(subtraction, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(57, 57, 57)
- .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGroup(layout.createSequentialGroup()
- .addGap(102, 102, 102)
- .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(multiplication, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(12, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void oneActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("1");
- if(firstDouble == null){
- firstDouble = 1;
- }
- /* double yolo = Double.parseDouble(result.getText());
- //int number = Integer.parseInt(result.getText());
- yolo++;
- System.out.println(Double.parseDouble("1"));
- result.setText(""+yolo+"");
- //result.setText(""+1+"");*/
- }
- private void twoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("2");
- }
- private void threeActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("3");
- }
- private void fourActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("4");
- }
- private void fiveActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("5");
- }
- private void sixActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("6");
- }
- private void sevenActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("7");
- }
- private void eightActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("8");
- }
- private void nineActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("9");
- }
- private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("0");
- }
- private void clearAllActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("");
- firstDouble = null;
- secondDouble = null;
- }
- private void resultActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void divisionActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- result.setText("/");
- }
- private void multiplicationActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void additionActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void subtractionActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void equalsActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- double answer = firstDouble + operator + secondDouble;
- if(firstDouble != null){
- if(operator != null){
- if(secondDouble != null){
- result.setText("(" + answer +")");
- }
- }
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(CalculatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new CalculatorGUI().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton addition;
- private javax.swing.JButton clear;
- private javax.swing.JButton clearAll;
- private javax.swing.JButton decimal;
- private javax.swing.JButton division;
- private javax.swing.JButton eight;
- private javax.swing.JButton equals;
- private javax.swing.JButton five;
- private javax.swing.JButton four;
- private javax.swing.JButton multiplication;
- private javax.swing.JButton nine;
- private javax.swing.JButton one;
- private javax.swing.JFormattedTextField result;
- private javax.swing.JButton seven;
- private javax.swing.JButton six;
- private javax.swing.JButton subtraction;
- private javax.swing.JButton three;
- private javax.swing.JButton two;
- private javax.swing.JButton zero;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement