1 2 Previous Next 23 Replies Latest reply: Oct 17, 2012 6:39 AM by 968683 Go to original post RSS
      • 15. Re: Java Calculator HELP!
        968683
        is this the result lable? if its wrong could you show me what would work?
        result = new JLabel(""); 
                  c.fill = GridBagConstraints.HORIZONTAL ;
                  c.gridx = 0;
                  c.gridy = 4;
                  c.gridwidth = 4;
        • 16. Re: Java Calculator HELP!
          abillconsl
          yes
          • 17. Re: Java Calculator HELP!
            968683
            than yes this is in it but it still wont display the code here is every thing that i have again
            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.*; 
            public class Calculator extends JFrame  {
            
                 JButton add ,subtract, multiply, divide; 
                 JTextField num1 , num2 ; 
                 JLabel result , enter1 , enter2 ; 
                 public Calculator () {
                      setLayout (new GridBagLayout());
                      GridBagConstraints c = new GridBagConstraints ();
                      
                      enter1 = new JLabel ("lst: ");
                      c.fill = GridBagConstraints.HORIZONTAL;
                      c.gridx = 0; 
                      c.gridy = 0;
                      add(enter1,c);
                      
                      num1 = new JTextField(10); 
                      c.fill = GridBagConstraints.HORIZONTAL;
                      c.gridx = 1;
                      c.gridy = 0; 
                      c.gridwidth = 3;
                      add(num1 , c ) ;
                      
                      enter2 = new JLabel ("2nd: ") ;
                      c.fill = GridBagConstraints.HORIZONTAL ; 
                      c.gridx = 0;
                      c.gridy = 1;
                      c.gridwidth = 1;
                      add (enter2 , c ) ;
                      
                      num2 = new  JTextField(10); 
                      c.fill = GridBagConstraints.HORIZONTAL ; 
                      c.gridx = 1;
                      c.gridy = 1; 
                      c.gridwidth = 3; 
                      add (num2 , c ) ; 
                      
                      add = new JButton ("+");
                      c.fill = GridBagConstraints.HORIZONTAL ; 
                      c.gridx = 0; 
                      c.gridy = 2;
                      c.gridwidth = 1;
                      add (add , c ) ; 
                      
                      subtract = new JButton ("-") ;
                      c.fill = GridBagConstraints.HORIZONTAL ;
                      c.gridx = 1; 
                      c.gridy = 2;
                      add (subtract , c ) ; 
                      
                      multiply = new JButton ("*") ; 
                      c.fill = GridBagConstraints.HORIZONTAL ;
                      c.gridx = 2;
                      c.gridy = 2;
                      add (multiply , c );
                      
                      divide = new JButton ("/"); 
                      c.fill = GridBagConstraints.HORIZONTAL ; 
                      c.gridx = 3;
                      c.gridy = 2;
                      add (divide , c); 
                      
                      result = new JLabel(""); 
                      c.fill = GridBagConstraints.HORIZONTAL ;
                      c.gridx = 0;
                      c.gridy = 4;
                      c.gridwidth = 4;
                      
                      event a = new event () ; 
                      add.addActionListener(a) ; 
                      subtract.addActionListener(a) ; 
                      multiply.addActionListener(a) ; 
                      divide.addActionListener(a) ; 
                       
                 
                 } 
                 public class event implements ActionListener {
                        public void actionPerformed(ActionEvent a) {
                           double number1 , number2 ;
                           
                      
                           try {
                                number1 = Double.parseDouble(num1.getText()) ; 
                           } catch (NumberFormatException e) {
                                result.setText("Illegal Data");
                                result.setForeground(Color.RED) ; 
                                return; 
                                
                                }
                           try {
                               number2 = Double.parseDouble(num2.getText()) ; 
                           }catch (NumberFormatException e) {
                               result.setText("illegal data");
                               result.setForeground(Color.RED);
                               return; 
                           }
                                     String op = a.getActionCommand();
                                     
                                     if (op.equals("+")) {
                                          double sum = number1 + number2 ;
                                          result.setText(number1 + "+" + number2 + "=" + sum ) ;
                                          result.setForeground(Color.RED) ; 
                                     } else if (op.equals("-")) {
                                          double diff = number1 - number2 ; 
                                          result.setText (number1 + "-" + number2 + "=" + diff) ;     
                                          result.setForeground(Color.RED);
                                     
                                     } else if (op.equals("*")) { 
                                          double factor = number1 * number2 ; 
                                          result.setText (number1 + "*" + number2 + "=" + factor) ;     
                                          result.setForeground(Color.RED);
                                     
                                          }else if (op.equals("/")) { 
                                               if (number2 == 0) {
                                                    result.setText("Cannot Divide by 0") ; 
                                                    result.setForeground(Color.RED);
                                               } else {
                                                    double quotient = number1 / number2 ; 
                                                    result.setText(number1 + "/" + number2 + "=" + quotient);
                                                    result.setForeground(Color.RED); 
                                               }
                                          }
                                               
                                }
                           }
                      public static void main (String args []) {
                           Calculator gui = new Calculator (); 
                           gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                           gui.setVisible(true); 
                           gui.setSize(250,175);
                           gui.setTitle("Event Night Product!"); 
                 }
                      }
            • 18. Re: Java Calculator HELP!
              abillconsl
              because you didn't add it. You need to code: add(result);
              • 19. Re: Java Calculator HELP!
                968683
                OMG THANKS IT WORKED I LOVE U
                • 20. Re: Java Calculator HELP!
                  abillconsl
                  You're welcome, but don't forget to thank doremifasollatido      - he said it first.
                  • 21. Re: Java Calculator HELP!
                    rukbat
                    Everyone might note that this thread was started by one student that seems to have abandoned their assignment the same day they started this thread:
                    https://forums.oracle.com/forums/profile.jspa?userID=957422
                    and the thread seems to have been hijacked by
                    https://forums.oracle.com/forums/profile.jspa?userID=965680
                    who has already had two of their posts on this topic locked (though that might be the cause of the hijack).


                    .

                    Edited by: rukbat on Oct 17, 2012 5:29 AM
                    Edit a typo.
                    • 22. Re: Java Calculator HELP!
                      abillconsl
                      Yes I noticed that.
                      • 23. Re: Java Calculator HELP!
                        968683
                        I lost my account and it wont let me login with all of the combinations i try so i had to make a new account!
                        1 2 Previous Next