Results 1 to 7 of 7

Thread: Updating Output

  1. #1
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Updating Output

    Ok so I wrote a program to calculate the max, min, and average of five numbers the user inputs. The only thing I want to do now is make it so that where my program displays "Result" it displays button related phrases. So if I press MAXIMUM it says "The Max is:", and if I then press "AVERAGE" it changes the text to "The Average is:". Does anyone know a simple way to do this?

    Here is my code:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MaxMinAvgGUI extends JFrame
    {
    	//frame
    	private static final int WIDTH  = 250;
       	private static final int HEIGHT = 280;
    
    	//component objects
    	private JLabel num1Label, num2Label, num3Label, num4Label, num5Label, blankLabel, resultLabel;
      	private JTextField num1TextField, num2TextField, num3TextField, num4TextField;   		private JTextField 
    
    num5TextField, resultTextField;
      	private JButton maxB, minB, avgB;
    
      	 //event handlers
      	 private MaxButtonHandler      maxbHandler;
      	 private MinButtonHandler minbHandler;
      	 private AvgButtonHandler avgbHandler;
    
       public MaxMinAvgGUI() //defines frame
       {
    	setTitle( "Max, Min, and Avg" ); //title of the frame
          	setSize( WIDTH, HEIGHT );   //frame size
    	
    	Container pane = getContentPane(); // get content pane
    
    	GridLayout aGrid = new GridLayout( 8, 2 ); //grid layout
    
    	pane.setLayout(aGrid);
    
         	 // JLabel objects
         	 num1Label   = new JLabel( "1st Number" );
         	 num2Label   = new JLabel( "2nd Number" );
         	 num3Label   = new JLabel( "3rd Number" );
         	 num4Label   = new JLabel( "4th Number" );
         	 num5Label   = new JLabel( "5th Number" );
          	 blankLabel = new JLabel( " " );
          	 resultLabel = new JLabel( "Result" );
    
         	 // JTextField objects
         	 num1TextField   = new JTextField( 10 );
         	 num2TextField   = new JTextField( 10 );
         	 num3TextField   = new JTextField( 10 );
         	 num4TextField   = new JTextField( 10 );
         	 num5TextField   = new JTextField( 10 );
          	 resultTextField = new JTextField( 10) ;
    
        	  // JButton objects
        	  maxB      = new JButton( "MAXIMUM" );
         	  maxbHandler      = new MaxButtonHandler();
         	  maxB.addActionListener(maxbHandler);
    
        	  minB = new JButton( "MINIMUM" );
         	  minbHandler      = new MinButtonHandler();
         	  minB.addActionListener(minbHandler);
    
        	  avgB = new JButton( "AVERAGE" );
         	  avgbHandler      = new AvgButtonHandler();
         	  avgB.addActionListener(avgbHandler);
    
        	  // add objects to the pane
        	  pane.add( num1Label );
        	  pane.add( num1TextField );
      	  pane.add( num2Label );
      	  pane.add( num2TextField );
        	  pane.add( num3Label );
        	  pane.add( num3TextField );
        	  pane.add( num4Label );
        	  pane.add( num4TextField );
        	  pane.add( num5Label );
        	  pane.add( num5TextField );
      	  pane.add( maxB );
         	  pane.add( minB );
         	  pane.add( blankLabel );
         	  pane.add( avgB );
         	  pane.add( resultLabel );
         	  pane.add( resultTextField );
    
          centerFrame( WIDTH, HEIGHT ); // center frame on screen
        } // end method
    
        public void centerFrame( int frameWidth, int frameHeight )
        {
           // create Toolkit object
           Toolkit aToolkit = Toolkit.getDefaultToolkit();
    
           // create Dimension object with user info
           Dimension screen = aToolkit.getScreenSize();
    
           // assign x, y position
           int xUpperLeftCorner = ( screen.width - frameWidth ) / 2;
           int yUpperLeftCorner = ( screen.height - frameHeight ) / 2;
    
           // method to position frame
           setBounds(xUpperLeftCorner, yUpperLeftCorner, frameWidth, frameHeight);
    
       } // end method
    
    private class MaxButtonHandler implements ActionListener
       {
          public void actionPerformed( ActionEvent e )
          {
             String num1Str   = num1TextField.getText();
             String num2Str   = num2TextField.getText();
             String num3Str   = num3TextField.getText();
             String num4Str   = num4TextField.getText();
             String num5Str   = num5TextField.getText();
             double num1      = Double.parseDouble( num1Str );
             double num2      = Double.parseDouble( num2Str );
             double num3      = Double.parseDouble( num3Str );
             double num4      = Double.parseDouble( num4Str );
             double num5      = Double.parseDouble( num5Str );
             double result    = Math.max(num1, Math.max(num2, Math.max(num3, Math.max(num4, num5)))); 
             String resultStr = Double.toString( result );
             resultTextField.setText( resultStr );
          }
       } // end class
    
    private class MinButtonHandler implements ActionListener
       {
          public void actionPerformed( ActionEvent e )
          {
             String num1Str   = num1TextField.getText();
             String num2Str   = num2TextField.getText();
             String num3Str   = num3TextField.getText();
             String num4Str   = num4TextField.getText();
             String num5Str   = num5TextField.getText();
             double num1      = Double.parseDouble( num1Str );
             double num2      = Double.parseDouble( num2Str );
             double num3      = Double.parseDouble( num3Str );
             double num4      = Double.parseDouble( num4Str );
             double num5      = Double.parseDouble( num5Str );
             double result    = Math.min(num1, Math.min(num2, Math.min(num3, Math.min(num4, num5)))); 
             String resultStr = Double.toString( result );
             resultTextField.setText( resultStr );
          }
       } // end class
    
    private class AvgButtonHandler implements ActionListener
       {
          public void actionPerformed( ActionEvent e )
          {
             String num1Str   = num1TextField.getText();
             String num2Str   = num2TextField.getText();
             String num3Str   = num3TextField.getText();
             String num4Str   = num4TextField.getText();
             String num5Str   = num5TextField.getText();
             double num1      = Double.parseDouble( num1Str );
             double num2      = Double.parseDouble( num2Str );
             double num3      = Double.parseDouble( num3Str );
             double num4      = Double.parseDouble( num4Str );
             double num5      = Double.parseDouble( num5Str );
             double result    = (num1 + num2 + num3 + num4 + num5)/5;
             String resultStr = Double.toString( result );
             resultTextField.setText( resultStr );
          }
       } // end class
    
       public static void main( String[] args )  // main method
       {
          JFrame aMaxMinAvgGUI = new MaxMinAvgGUI();  // create the frame object
          aMaxMinAvgGUI.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          aMaxMinAvgGUI.setVisible( true );               // display the frame
       } // end main
    
    } // end class

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Updating Output

    If I understand the question you would like "Result" to change to "The Maximum is:..."You can add a couple statements into your methods (AvgButtonHandler,MinButtonHandler,MaxButtonHandle r) that state what you want the label to say. ie...
    Java Code:
     String num1Str   = num1TextField.getText();
             String num2Str   = num2TextField.getText();
             String num3Str   = num3TextField.getText();
             String num4Str   = num4TextField.getText();
             String num5Str   = num5TextField.getText();
             double num1      = Double.parseDouble( num1Str );
             double num2      = Double.parseDouble( num2Str );
             double num3      = Double.parseDouble( num3Str );
             double num4      = Double.parseDouble( num4Str );
             double num5      = Double.parseDouble( num5Str );
             double result    = Math.max(num1, Math.max(num2, Math.max(num3, Math.max(num4, num5))));
             resultLabel.setText("The Maximum is: ");// <--CHANGES THE RESULT LABEL ON BUTTON CLICK
             String resultStr = Double.toString( result );
             resultTextField.setText( resultStr );
    This will change the result label to what you would like it to say on the event of clicking the button.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Updating Output

    @Yokomoko: You application is very good.

    In actionPerformed methods inside MaxButtonHandler, MinButtonHandler and AvgButtonHandler private classes you should add this line of code:

    Java Code:
    resultLabel.setText(" /* your string instead of string Result goes here */ ");
    @jhuber151: Thanks for great reply. You were faster than me ;-)

  4. #4
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Updating Output

    Wow thank you guys. I knew I could do something in the method but I wasn't sure on what code to use. Thanks a ton guys!!!

  5. #5
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Updating Output

    I have changed the coding but it is not updating the text. I am at a loss as to what to do

  6. #6
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Updating Output

    Nevermind I fixed it. Thanks again!

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Updating Output

    ^You could maybe for the practice put another button in empty cell in your application that clear all values of text fields.

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 11:39 AM
  2. Updating JDK
    By sandz24 in forum New To Java
    Replies: 4
    Last Post: 01-20-2012, 03:34 AM
  3. Updating my gui
    By mrx89_7 in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 05:33 AM
  4. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

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