Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New to Java, need help with GUI

    I cant seem to figure out how to get my buttons to perform the proper calculations. Can some one please help.


    Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class LetsDoMath extends JFrame {
    
    		JLabel instruction;
    		JTextField numberField;
    		JButton squareIt;
            JButton squareRoot;
            JButton cube;
            JButton divide;
    		
    		/**
    		 * Constructor of LetsDoMath
    		 *
    		 */
    		public LetsDoMath() {
    			setSize(400, 100);
    			setTitle("Some Math Functions");
    			JPanel panel = new JPanel();
    			add(panel);
    			
    			instruction = new JLabel("Enter you number");
    			panel.add(instruction);
    			
    			numberField = new JTextField(25);
    			panel.add(numberField);
    			
    
    			squareIt = new JButton("SQUARE");
    			squareIt.addActionListener(new ButtonListener());
    			panel.add(squareIt);
    			
    			squareRoot = new JButton("SQUARE ROOT");
    			squareRoot.addActionListener(new ButtonListener());
    			panel.add(squareRoot);
    			
    			
    			cube = new JButton("CUBE");
    			cube.addActionListener(new ButtonListener());
    			panel.add(cube);
    			
    			divide = new JButton("1/x");
    			divide.addActionListener(new ButtonListener());
    			panel.add(divide);// add the other 3 buttons here
    			
    			setVisible(true);
    		}
    	
    	
    	public class ButtonListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			String msg = e.getActionCommand();
    			// more work is needed here to make the buttons
    			// functional. 
    			
    			
    		//from here down is obviously wrong, I can not figure out how to make it work	
    		    String input;
    			
    			double result = 0.0;
    	
    			input = numberField.getText();
    			
    			if(msg.equals("squareIt"))
    			{
    				result = Double.parseDouble(input);//want to use math.pow 
    			}
    			
    			else if(msg.equals("squareRoot"))
    			{
    				result = Double.parseDouble(input);//want to use Math.sqrt but don't know how to fit it in
    			}
    			
    			else if(msg.equals("cube"))
    			{
    				result = Double.parseDouble(input);//want to use Math.pow here but dont know how
    			}
    			
    			else if(msg.equals("divide"))
    			{
    				result = Double.parseDouble(input);//1 divided by input
    			}	
    			
    		}
    	}
    }
    Last edited by monesabsolut; 01-27-2011 at 04:46 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This won't do anything.
    The buttonListener doesn't actually do any tasks; the getActionCommand is the actual text that will show up on the button.
    Next to this though, all you are doing is casting the value. You need to actually operate on it somehow.
    I would take a different approach to this.
    PHP Code:
        public interface ILetsDoMathCalcs
        
    {
            public 
    double calculate(double in);
        }
        
        public class 
    LDMSquare implements ILetsDoMathCalcs
        
    {
            public 
    double calculate(double in)
            {
                return 
    Math.pow(in2);
            }
        }
        
        public class 
    LDMSquareRoot implements ILetsDoMathCalcs
        
    {
            public 
    double calculate(double in)
            {
                return 
    Math.sqrt(in);
            }
        }
        
        public class 
    LDMCube implements ILetsDoMathCalcs
        
    {
            public 
    double calculate(double in)
            {
                return 
    Math.pow(in3);
            }
        }
        
        public class 
    LDMDivide implements ILetsDoMathCalcs
        
    {
            public 
    double calculate(double in)
            {
                return 
    in;
            }
        }
        
        public class 
    ExecuteCalc implements ActionListener
        
    {
            private 
    ILetsDoMathCalcs calc;
            
            public 
    ExecuteCalc(ILetsDoMathCalcs calc)
            {
                
    this.calc calc;
            }
            
            public 
    void actionPerformed(ActionEvent e)
            {
                
    double result 0.0;
                try
                {
                    
    result this.calc.calculate(Double.parseDouble(numberField.getText()));
                }
                catch (
    Exception ex)
                {
                    
    System.err.println("Invalid entry");
                }
                
    // Not sure what you want to do with the result, actionPerformed won't return a result so I'll just put it into the numberField
                
    numberField.setText(String.valueOf(result));
            }
        } 
    The listeners are only created for unary type operators with a single number, but you can easily play with interfaces to get around that. Then you simply snap them in place for your button listeners:
    PHP Code:
    squareIt.addActionListener(new ExecuteCalc(new LDMSquare())); 
    Where the provided ILetsDoMath class to ExecuteCalc matches your desired operation. You may want to consider doing a bit more with the ExecuteCalc class to pass values into it instead of relying on the outer class variables instead, but you can do that by passing it into the constructor.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •