Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Looking for assistance adding a reset button to my GUI

    I'm writing a program for my Java class, I have written and tested it successfully, however I am attempting to go above and beyond the requirements by adding a third button to clear all fields into the code. Here is the assignment: Design a GUI program to find the weighted average of four test scores. The four test scores and their respective weights are given in the following format:

    testscore1 weight1
    ...

    For example, the sample data is as follows:

    75 0.20
    95 0.35
    85 0.15
    65 0.30

    The user is supposed to enter the data and press a Calculate button. The program must display the weighted average.

    As I said I have completed the assignment and it seems to work properly, here is what I have written, when I compile I get an error "Evaluate_7.java:164: error: ';' expected"
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
       
       public class Evaluate_7 extends JFrame
       {
        
       private JLabel  blankoneL, blanktwoL, blankthreeL, 
                       
               scoreL, weightL, oneL, twoL, threeL,fourL,averageL;
        
       
       private JTextField  oneTF, twoTF, threeTF, fourTF, woneTF, wtwoTF,
                            
               wthreeTF, wfourTF, averageTF;
        
                         
       private JButton submitB, resetB, exitB;
        
       private SubmitButtonHandler sbHandler;
       private ResetButtonHandler
    rbHandler;    
       private ExitButtonHandler ebHandler;
       //indicate the size of the window
        
       private static final int WIDTH = 500;
        
       private static final int HEIGHT = 400;
       public Evaluate_7() 
       { 
       //Create labels
            
       blankoneL = new JLabel("");
            
       blanktwoL = new JLabel("");
            
       blankthreeL = new JLabel("");
            
       scoreL = new JLabel("Score", SwingConstants.CENTER);//swingConstants." " tells the program how to align the text in the window
            
       weightL = new JLabel("Weight", SwingConstants.CENTER);
            
       oneL = new JLabel("Test Score One: ", SwingConstants.RIGHT);
            
       twoL = new JLabel("Test Score Two: ", SwingConstants.RIGHT);
            
       threeL = new JLabel("Test Score Three: ", SwingConstants.RIGHT);
            
       fourL = new JLabel("Test Score Four: ", SwingConstants.RIGHT);
            
       averageL = new JLabel("Weighted Average: ", SwingConstants.RIGHT);
                
       //Create textfields
            
       oneTF = new JTextField(5);
            
       twoTF = new JTextField(5);
            
       threeTF = new JTextField(5);
            
       fourTF = new JTextField(5);
            
       woneTF = new JTextField(5);
            
       wtwoTF = new JTextField(5);
            
       wthreeTF = new JTextField(5);
            
       wfourTF = new JTextField(5);
            
       averageTF = new JTextField(5);
                
       //create Submit Button
            
       submitB = new JButton("Submit");
            
       sbHandler = new SubmitButtonHandler();
            
       submitB.addActionListener(sbHandler);
       //Create Reset Button
       resetB = new JButton("Reset");
       rbHandler = new ResetButtonHandler();
       resetB.addActionListener(rbHandler);
                
       //Create Exit Button
            
       exitB = new JButton("Exit");
            
       ebHandler = new ExitButtonHandler();
            
       exitB.addActionListener(ebHandler);
                
       //Set the title of the window
            
       setTitle("Evaluate 6 IT145");
                
       //Get the container
            
       Container pane = getContentPane();
                    
       //Set the layout
            
       pane.setLayout(new GridLayout(7,3));
                
       //Place the components in the pane
            
       pane.add(blankoneL);
            
       pane.add(scoreL);
            
       pane.add(weightL); 
       
             
       pane.add(oneL);
         
       pane.add(oneTF);
            
       pane.add(woneTF); 
       
            
       pane.add(twoL);
            
       pane.add(twoTF);
            
       pane.add(wtwoTF);
                
       pane.add(threeL);
            
       pane.add(threeTF);
            
       pane.add(wthreeTF); 
               
       pane.add(fourL);
            
       pane.add(fourTF);
            
       pane.add(wfourTF);
       
       pane.add(averageL);
            
       pane.add(averageTF);
            
       pane.add(blanktwoL); 
       
       pane.add(submitB);
            
       pane.add(resetB);
            
       pane.add(exitB); 
       //set the size of the window and display it
            
       setSize(WIDTH, HEIGHT);
            
       setVisible(true);
            
       setDefaultCloseOperation(EXIT_ON_CLOSE);
        
       }
        
       private class SubmitButtonHandler implements ActionListener
       {
            
       public void actionPerformed(ActionEvent e)
       
                
       private class ResetButtonHandler implements ActionListener
    	{
    	public void actionPerformed(ActionEvent e)
    	{
       double one, wone, two, wtwo,
                        
                three, wthree, four, wfour, ave;
                
                
       DecimalFormat twoDigits = new DecimalFormat("0.00");
                
       one = Double.parseDouble(oneTF.getText());
                
       wone = Double.parseDouble(woneTF.getText());
                
       two = Double.parseDouble(twoTF.getText());
                
       wtwo = Double.parseDouble(wtwoTF.getText());
                
       three = Double.parseDouble(threeTF.getText());
                
       wthree = Double.parseDouble(wthreeTF.getText());
                
       four = Double.parseDouble(fourTF.getText());
                
       wfour = Double.parseDouble(wfourTF.getText());
                
       ave = (one * wone + two * wtwo + three * wthree
                     
             + four * wfour)/(wone+wtwo+wthree+wfour);
                
       averageTF.setText(""+ twoDigits.format(ave));
            
       }
        
       }
        
       private class ExitButtonHandler implements ActionListener
       {
            
       public void actionPerformed(ActionEvent e)
       {
                
       System.exit(0);
            
       }
        
       }
        
       public static void main(String[] args) 
       {
            
       Evaluate_7 wAveObject = new Evaluate_7();
        
       }
       }
       }

  2. #2
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Looking for assistance adding a reset button to my GUI

    Hard to see without indentation, but have you not closed the braces for SubmitButtonHandler or actionPerformed?

  3. #3
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: Looking for assistance adding a reset button to my GUI

    Sorry, I cleaned it up I agree its near impossible to see that way. So the code works without the reset button portion it complies and runs perfectly but as soon as I add in the reset button I am getting 2 errors (not sure where the second came from)

    Evaluate_6_1.java:194: error: illegal start of expression
    private class ResetButtonHandler implements ActionListener
    ^
    Evaluate_6_1.java:198: error: ';' expected
    public void actionPerformed(ActionEvent e)

    Here is the cleaned up code (I saved a second copy of the code for comparison hence the Evaluate_6_1...
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
       
       public class Evaluate_6_1 extends JFrame
       {
        
          private JLabel  blankoneL, blanktwoL, 
                              blankthreeL, scoreL, 
                              weightL, oneL, twoL, 
                              threeL,fourL,averageL;
        
          
          private JTextField  oneTF, twoTF, threeTF,
                                  fourTF, woneTF, wtwoTF,
                            
                                  wthreeTF, wfourTF, averageTF;
        
                            
          private JButton calculateB, exitB;
        
          private CalculateButtonHandler cbHandler;
          private ResetButtonHandler rbHandler;
        
          private ExitButtonHandler ebHandler;
          
          //indicate the size of the window
        
          private static final int WIDTH = 500;
        
          private static final int HEIGHT = 400;
        
          
          public Evaluate_6_1() 
          { 
              //Create labels
            
              blankoneL = new JLabel("");
            
              blanktwoL = new JLabel("");
            
              blankthreeL = new JLabel("");
            
              scoreL = new JLabel("Score", SwingConstants.CENTER);//swingConstants.XXXX tells the program how to align the text in the window
            
              weightL = new JLabel("Weight", SwingConstants.CENTER);
            
              oneL = new JLabel("Test Score One: ", SwingConstants.RIGHT);
            
              twoL = new JLabel("Test Score Two: ", SwingConstants.RIGHT);
            
              threeL = new JLabel("Test Score Three: ", SwingConstants.RIGHT);
            
              fourL = new JLabel("Test Score Four: ", SwingConstants.RIGHT);
            
              averageL = new JLabel("Weighted Average: ", SwingConstants.RIGHT);
                
          
              //Create textfields
            
              oneTF = new JTextField(5);
            
              twoTF = new JTextField(5);
            
              threeTF = new JTextField(5);
            
              fourTF = new JTextField(5);
            
              woneTF = new JTextField(5);
            
              wtwoTF = new JTextField(5);
            
              wthreeTF = new JTextField(5);
            
              wfourTF = new JTextField(5);
            
              averageTF = new JTextField(5);
                
              
              //create Submit Button
            
              calculateB = new JButton("Submit");
            
              cbHandler = new CalculateButtonHandler();
            
              calculateB.addActionListener(cbHandler);
              
              //Create Reset Button
       
              resetB = new JButton("Reset");
       
              rbHandler = new ResetButtonHandler();
       
              resetB.addActionListener(rbHandler);
    
                
          
              //Create Exit Button
            
              exitB = new JButton("Exit");
            
              ebHandler = new ExitButtonHandler();
            
              exitB.addActionListener(ebHandler);
                
          
              //Set the title of the window
            
              setTitle("Evaluate 6 IT145");
                
          
              //Get the container
            
              Container pane = getContentPane();
                    
             
              //Set the layout
            
              pane.setLayout(new GridLayout(7,3));
                
              
              //Place the components in the pane
            
              pane.add(blankoneL);
            
              pane.add(scoreL);
            
              pane.add(weightL); 
             
              //first row
             
              pane.add(oneL);
         
              pane.add(oneTF);
            
              pane.add(woneTF); 
          
              //second row
            
              pane.add(twoL);
            
              pane.add(twoTF);
            
              pane.add(wtwoTF);
              //third row
             
              pane.add(threeL);
            
              pane.add(threeTF);
            
              pane.add(wthreeTF); 
          
              //fourth row
            
              pane.add(fourL);
            
              pane.add(fourTF);
            
              pane.add(wfourTF);
          
              //fifth row
              pane.add(averageL);
            
              pane.add(averageTF);
            
              pane.add(blanktwoL); 
          
              //sixth row
              pane.add(calculateB);
            
              pane.add(blankthreeL);
            
          
              //seventh row
              pane.add(exitB); 
              
              //set the size of the window and display it
            
              setSize(WIDTH, HEIGHT);
            
              setVisible(true);
            
              setDefaultCloseOperation(EXIT_ON_CLOSE);
        
          }
        
          
          private class CalculateButtonHandler implements ActionListener
              {
            
              public void actionPerformed(ActionEvent e)
              {
          private class ResetButtonHandler implements ActionListener
    	
              {
    	
              public void actionPerformed(ActionEvent e)
    	
              }
    
                
              double one, wone, two, wtwo,
                        
                      three, wthree, four, wfour, ave;
                
                   
              DecimalFormat twoDigits = new DecimalFormat("0.00");
                
              one = Double.parseDouble(oneTF.getText());
                
              wone = Double.parseDouble(woneTF.getText());
                
              two = Double.parseDouble(twoTF.getText());
                
              wtwo = Double.parseDouble(wtwoTF.getText());
                
              three = Double.parseDouble(threeTF.getText());
                
              wthree = Double.parseDouble(wthreeTF.getText());
                
              four = Double.parseDouble(fourTF.getText());
                
              wfour = Double.parseDouble(wfourTF.getText());
                
              ave = (one * wone + two * wtwo + three * wthree
                     
                   + four * wfour)/(wone+wtwo+wthree+wfour);
                
              averageTF.setText(""+ twoDigits.format(ave));
            
              }
        
          }
        
          
          private class ExitButtonHandler implements ActionListener
          {
            
              public void actionPerformed(ActionEvent e)
              {
                
                  System.exit(0);
            
              }
        
          }
        
          public static void main(String[] args) 
          {
            
          
              Evaluate_6_1 wAveObject = new Evaluate_6();
        
          }
       }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Looking for assistance adding a reset button to my GUI

    You can't declare a private class within a method, but why are you even trying to do this in the first place? Your code also suffers from your not compiling often enough. If you don't use an IDE, you should compile your code early and often, and fix all compilation errors before adding any new code. Also your indentations are still off making your code hard to read.

  5. #5
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: Looking for assistance adding a reset button to my GUI

    I am using an IDE, I'm using JGrasp...I did fix all errors prior to adding to the code as I mentioned it works perfect without the Reset button code. I am adding it so the user can clear the contents of the fields and start over, rather than manually delete all the info.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Looking for assistance adding a reset button to my GUI

    You have still not addressed my main concern though: why are you trying to place a private class within a method? Fix this and you will likely fix your code.

Similar Threads

  1. Restart/Reset Button
    By PlumApps in forum New To Java
    Replies: 3
    Last Post: 05-04-2012, 07:47 AM
  2. Replies: 11
    Last Post: 03-14-2012, 08:30 PM
  3. Clear/Reset Button Problem
    By Ryan10 in forum New To Java
    Replies: 10
    Last Post: 04-12-2011, 04:04 PM
  4. Reset form button and duplicated data
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 05:40 AM
  5. <html:reset> button in struts
    By Satheeshhere in forum Web Frameworks
    Replies: 1
    Last Post: 07-15-2008, 04:14 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
  •